Skip to content

Commit

Permalink
const version of try_from_array_len, and some missing inlines
Browse files Browse the repository at this point in the history
  • Loading branch information
Lokathor committed Jan 2, 2025
1 parent 9cd6544 commit 6e7f730
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/arrayvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,7 @@ impl<A: Array> ArrayVec<A> {
/// If the given length is greater than the capacity of the array this will
/// error, and you'll get the array back in the `Err`.
#[inline]
#[cfg(not(feature = "latest_stable_rust"))]
pub fn try_from_array_len(data: A, len: usize) -> Result<Self, A> {
/* Note(Soveu): Should we allow A::CAPACITY > u16::MAX for now? */
if len <= A::CAPACITY {
Expand All @@ -1079,6 +1080,26 @@ impl<A: Array> ArrayVec<A> {
Err(data)
}
}

/// Wraps an array, using the given length as the starting length.
///
/// If you want to use the whole length of the array, you can just use the
/// `From` impl.
///
/// ## Failure
///
/// If the given length is greater than the capacity of the array this will
/// error, and you'll get the array back in the `Err`.
#[inline]
#[cfg(feature = "latest_stable_rust")]
pub const fn try_from_array_len(data: A, len: usize) -> Result<Self, A> {
/* Note(Soveu): Should we allow A::CAPACITY > u16::MAX for now? */
if len <= A::CAPACITY {
Ok(Self { data, len: len as u16 })
} else {
Err(data)
}
}
}

impl<A> ArrayVec<A> {
Expand Down Expand Up @@ -1883,6 +1904,7 @@ impl<A: Array> ArrayVec<A> {
/// assert_eq!(v, &[1, 2, 3]);
/// assert_eq!(v.capacity(), 13);
/// ```
#[inline]
#[cfg(feature = "rustc_1_57")]
pub fn try_drain_to_vec_and_reserve(
&mut self, n: usize,
Expand Down Expand Up @@ -1925,6 +1947,7 @@ impl<A: Array> ArrayVec<A> {
/// // Vec may reserve more than necessary in order to prevent more future allocations.
/// assert!(v.capacity() >= 3);
/// ```
#[inline]
#[cfg(feature = "rustc_1_57")]
pub fn try_drain_to_vec(&mut self) -> Result<Vec<A::Item>, TryReserveError> {
self.try_drain_to_vec_and_reserve(0)
Expand Down

0 comments on commit 6e7f730

Please sign in to comment.