Add into_inner method (#124)

* Add into_inner method

* Whoops, forgot the function
This commit is contained in:
Johnny
2020-10-13 11:07:20 -07:00
committed by GitHub
parent 1e5c52fab7
commit ee3e73ccc6
6 changed files with 8755 additions and 1507 deletions
+3 -2
View File
@@ -35,8 +35,9 @@ pub trait Array {
/// `CAPACITY` value.
fn as_slice_mut(&mut self) -> &mut [Self::Item];
/// Create a default-initialized instance of ourself, similar to the [`Default`] trait, but
/// implemented for the same range of sizes as [`Array`].
/// Create a default-initialized instance of ourself, similar to the
/// [`Default`] trait, but implemented for the same range of sizes as
/// [`Array`].
fn default() -> Self;
}
+8675 -1474
View File
File diff suppressed because it is too large Load Diff
+67 -19
View File
@@ -64,9 +64,9 @@ macro_rules! array_vec {
///
/// ## Construction
///
/// You can use the `array_vec!` macro similarly to how you might use the `vec!` macro.
/// Specify the array type, then optionally give all the initial values you want
/// to have.
/// You can use the `array_vec!` macro similarly to how you might use the `vec!`
/// macro. Specify the array type, then optionally give all the initial values
/// you want to have.
/// ```rust
/// # use tinyvec::*;
/// let some_ints = array_vec!([i32; 4] => 1, 2, 3);
@@ -106,10 +106,7 @@ pub struct ArrayVec<A: Array> {
impl<A: Array> Default for ArrayVec<A> {
fn default() -> Self {
Self {
len: 0,
data: A::default(),
}
Self { len: 0, data: A::default() }
}
}
@@ -223,8 +220,7 @@ impl<A: Array> ArrayVec<A> {
/// ```
#[inline]
pub fn try_append<'other>(
&mut self,
other: &'other mut Self,
&mut self, other: &'other mut Self,
) -> Option<&'other mut Self> {
let new_len = self.len() + other.len();
if new_len > A::CAPACITY {
@@ -322,6 +318,64 @@ impl<A: Array> ArrayVec<A> {
ArrayVecDrain::new(self, range)
}
/// Returns the inner backing storage of the `ArrayVec`. If this `ArrayVec` is
/// full, every element in the array will be used. If this `ArrayVec` is not
/// full, elements outside of the `ArrayVec`'s bounds with be set to their
/// default value.
///
/// ## Example
///
/// ```rust
/// # use tinyvec::{array_vec, ArrayVec};
/// let mut favorite_numbers = array_vec!([i32; 5] => 87, 48, 33, 9, 26);
/// assert_eq!(favorite_numbers.clone().into_inner(), [87, 48, 33, 9, 26]);
///
/// favorite_numbers.pop();
/// assert_eq!(favorite_numbers.into_inner(), [87, 48, 33, 9, 0]);
/// ```
///
/// A use for this function is to build an array from an iterator by first
/// collecting it into an `ArrayVec`.
///
/// ```rust
/// # use tinyvec::ArrayVec;
/// #
/// # struct FibonacciIterator {
/// # t1: i32,
/// # t2: i32,
/// # }
/// #
/// # impl FibonacciIterator {
/// # fn new() -> Self {
/// # Self { t1: 0, t2: 1 }
/// # }
/// # }
/// #
/// # impl Iterator for FibonacciIterator {
/// # type Item = i32;
/// #
/// # fn next(&mut self) -> Option<i32> {
/// # let res = self.t1;
/// # let next = self.t1 + self.t2;
/// # self.t1 = self.t2;
/// # self.t2 = next;
/// # Some(res)
/// # }
/// # }
/// // collect the first 10 numbers of the fibonacci sequence into an array,
/// // with a convenient one-liner
/// let fib_array: [i32; 10] =
/// FibonacciIterator::new()
/// .take(10)
/// .collect::<ArrayVec<[i32; 10]>>()
/// .into_inner();
/// assert_eq!(fib_array, [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]);
/// ```
#[inline]
pub fn into_inner(self) -> A {
self.data
}
/// Clone each element of the slice into this `ArrayVec`.
///
/// ## Panics
@@ -375,8 +429,7 @@ impl<A: Array> ArrayVec<A> {
/// ```
#[inline]
pub fn fill<I: IntoIterator<Item = A::Item>>(
&mut self,
iter: I,
&mut self, iter: I,
) -> I::IntoIter {
let mut iter = iter.into_iter();
for element in iter.by_ref().take(self.capacity() - self.len()) {
@@ -629,9 +682,7 @@ impl<A: Array> ArrayVec<A> {
/// ```
#[inline]
pub fn resize_with<F: FnMut() -> A::Item>(
&mut self,
new_len: usize,
mut f: F,
&mut self, new_len: usize, mut f: F,
) {
match new_len.checked_sub(self.len as usize) {
None => self.truncate(new_len),
@@ -787,9 +838,7 @@ impl<A: Array> ArrayVec<A> {
/// ```
#[inline]
pub fn splice<R, I>(
&mut self,
range: R,
replacement: I,
&mut self, range: R, replacement: I,
) -> ArrayVecSplice<'_, A, core::iter::Fuse<I::IntoIter>>
where
R: RangeBounds<usize>,
@@ -1558,8 +1607,7 @@ where
type Value = ArrayVec<A>;
fn expecting(
&self,
formatter: &mut core::fmt::Formatter,
&self, formatter: &mut core::fmt::Formatter,
) -> core::fmt::Result {
formatter.write_str("a sequence")
}
+6 -3
View File
@@ -4,7 +4,10 @@
feature = "nightly_slice_partition_dedup",
feature(slice_partition_dedup)
)]
#![cfg_attr(feature = "nightly_const_generics", feature(min_const_generics, array_map))]
#![cfg_attr(
feature = "nightly_const_generics",
feature(min_const_generics, array_map)
)]
#![cfg_attr(docs_rs, feature(doc_cfg))]
#![warn(clippy::missing_inline_in_public_items)]
#![warn(clippy::must_use_candidate)]
@@ -40,8 +43,8 @@
//! * `rustc_1_40` makes the crate assume a minimum rust version of `1.40.0`,
//! which allows some better internal optimizations.
//! * `serde` provides a `Serialize` and `Deserialize` implementation for
//! [`TinyVec`] and [`ArrayVec`] types, provided the inner item also
//! has an implementation.
//! [`TinyVec`] and [`ArrayVec`] types, provided the inner item also has an
//! implementation.
//!
//! ## API
//! The general goal of the crate is that, as much as possible, the vecs here
+1 -2
View File
@@ -152,8 +152,7 @@ impl<'s, T> SliceVec<'s, T> {
/// ```
#[inline]
pub fn drain<'p, R: RangeBounds<usize>>(
&'p mut self,
range: R,
&'p mut self, range: R,
) -> SliceVecDrain<'p, 's, T>
where
T: Default,
+3 -7
View File
@@ -554,8 +554,7 @@ impl<A: Array> TinyVec<A> {
/// ```
#[inline]
pub fn drain<R: RangeBounds<usize>>(
&mut self,
range: R,
&mut self, range: R,
) -> TinyVecDrain<'_, A> {
match self {
TinyVec::Inline(i) => TinyVecDrain::Inline(i.drain(range)),
@@ -803,9 +802,7 @@ impl<A: Array> TinyVec<A> {
/// ```
#[inline]
pub fn splice<R, I>(
&mut self,
range: R,
replacement: I,
&mut self, range: R, replacement: I,
) -> TinyVecSplice<'_, A, core::iter::Fuse<I::IntoIter>>
where
R: RangeBounds<usize>,
@@ -1452,8 +1449,7 @@ where
type Value = TinyVec<A>;
fn expecting(
&self,
formatter: &mut core::fmt::Formatter,
&self, formatter: &mut core::fmt::Formatter,
) -> core::fmt::Result {
formatter.write_str("a sequence")
}