mirror of
https://github.com/openharmony/third_party_rust_tinyvec.git
synced 2026-07-19 14:23:33 -04:00
remove the try_push thing
This commit is contained in:
+17
-23
@@ -80,6 +80,19 @@ impl<A: Array, I: SliceIndex<[A::Item]>> IndexMut<I> for ArrayVec<A> {
|
||||
|
||||
impl<A: Array> ArrayVec<A> {
|
||||
/// Move all values from `other` into this vec.
|
||||
///
|
||||
/// ## Panics
|
||||
/// * If the vec overflows its capacity
|
||||
///
|
||||
/// ## Example
|
||||
/// ```rust
|
||||
/// use tinyvec::*;
|
||||
/// let mut av = array_vec!([i32; 10], 1, 2, 3);
|
||||
/// let mut av2 = array_vec!([i32; 10], 4, 5, 6);
|
||||
/// av.append(&mut av2);
|
||||
/// assert_eq!(av, &[1, 2, 3, 4, 5, 6][..]);
|
||||
/// assert_eq!(av2, &[][..]);
|
||||
/// ```
|
||||
#[inline]
|
||||
pub fn append(&mut self, other: &mut Self) {
|
||||
for item in other.drain(..) {
|
||||
@@ -339,12 +352,14 @@ impl<A: Array> ArrayVec<A> {
|
||||
|
||||
/// Place an element onto the end of the vec.
|
||||
///
|
||||
/// See also, [`try_push`](ArrayVec::try_push)
|
||||
/// ## Panics
|
||||
/// * If the length of the vec would overflow the capacity.
|
||||
#[inline(always)]
|
||||
pub fn push(&mut self, val: A::Item) {
|
||||
if self.try_push(val).is_err() {
|
||||
if self.len < A::CAPACITY {
|
||||
replace(&mut self.data.as_slice_mut()[self.len], val);
|
||||
self.len += 1;
|
||||
} else {
|
||||
panic!("ArrayVec: overflow!")
|
||||
}
|
||||
}
|
||||
@@ -580,27 +595,6 @@ impl<A: Array> ArrayVec<A> {
|
||||
Err(data)
|
||||
}
|
||||
}
|
||||
|
||||
/// Pushes an item if there's room.
|
||||
///
|
||||
/// ## Failure
|
||||
///
|
||||
/// If there's no more capacity the vec is unchanged, and you get the item
|
||||
/// back in the `Err`.
|
||||
#[inline]
|
||||
pub fn try_push(&mut self, val: A::Item) -> Result<(), A::Item> {
|
||||
if self.len < A::CAPACITY {
|
||||
replace(&mut self.data.as_slice_mut()[self.len], val);
|
||||
self.len += 1;
|
||||
Ok(())
|
||||
} else {
|
||||
Err(val)
|
||||
}
|
||||
}
|
||||
|
||||
// LATER: try_insert ?
|
||||
|
||||
// LATER: try_remove ?
|
||||
}
|
||||
|
||||
/// Draining iterator for `ArrayVecDrain`
|
||||
|
||||
+11
-14
@@ -23,18 +23,15 @@
|
||||
//! types](https://doc.rust-lang.org/std/default/trait.Default.html#implementors),
|
||||
//! so I think that you'll find these vecs useful in many cases.
|
||||
//!
|
||||
//! * [`ArrayVec`](ArrayVec::<A>) is an array-backed vec-like structure with a
|
||||
//! fixed capacity. If you try to grow the length past the array's capacity it
|
||||
//! will error or panic (depending on the method used).
|
||||
//! * [`TinyVec`](TinyVec::<A>) is an enum that's either an "inline" `ArrayVec`
|
||||
//! or a "heap" `Vec`. If it's in array mode and you try to grow the vec
|
||||
//! beyond its capacity it'll quietly transition into heap mode for you and
|
||||
//! then continue operation. This type is naturally behind the `alloc` feature
|
||||
//! gate.
|
||||
//! * [`ArrayVec`](ArrayVec) is an array-backed vec-like structure with a fixed
|
||||
//! capacity. If you try to grow the length past the array's capacity it will
|
||||
//! error or panic (depending on the method used).
|
||||
//! * (`alloc` feature) [`TinyVec`](TinyVec) is an enum that's either an
|
||||
//! "Inline" `ArrayVec` or a "Heap" `Vec`. If it's Inline and you try to grow
|
||||
//! the `ArrayVec` beyond its array capacity it will quietly transition into
|
||||
//! Heap mode and then continue the operation.
|
||||
//!
|
||||
//! ## Stability Goal
|
||||
//!
|
||||
//! The crate is still in development, but we have some very clear goals:
|
||||
//! ## Crate Goals
|
||||
//!
|
||||
//! 1) The crate is 100% safe code. Not just a safe API, there are also no
|
||||
//! `unsafe` internals. `#![forbid(unsafe_code)]`.
|
||||
@@ -44,10 +41,10 @@
|
||||
//! 3) The intended API is that, _as much as possible_, these types are
|
||||
//! essentially a "drop-in" replacement for the standard [`Vec`](Vec::<T>)
|
||||
//! type.
|
||||
//! * Stable `Vec` methods that the vecs here also have should have the exact
|
||||
//! same signature.
|
||||
//! * Stable `Vec` methods that the vecs here also have should be the same
|
||||
//! general signature.
|
||||
//! * Unstable `Vec` methods are sometimes provided via a crate feature, but
|
||||
//! if so they also require Nightly.
|
||||
//! if so they also require a Nightly compiler.
|
||||
//! * Some methods are provided that _are not_ part of the `Vec` type, such
|
||||
//! as additional constructor methods. In this case, the names are rather
|
||||
//! long and whimsical in the hopes that they don't class with any possible
|
||||
|
||||
@@ -371,8 +371,6 @@ impl<A: Array> TinyVec<A> {
|
||||
}
|
||||
|
||||
/// Place an element onto the end of the vec.
|
||||
///
|
||||
/// See also, [`try_push`](TinyVec::<A>::try_push)
|
||||
/// ## Panics
|
||||
/// * If the length of the vec would overflow the capacity.
|
||||
#[inline(always)]
|
||||
@@ -580,12 +578,6 @@ impl<A: Array> TinyVec<A> {
|
||||
let arr = ArrayVec::try_from_array_len(data, len)?;
|
||||
Ok(TinyVec::Inline(arr))
|
||||
}
|
||||
|
||||
// LATER: try_push ?
|
||||
|
||||
// LATER: try_insert ?
|
||||
|
||||
// LATER: try_remove ?
|
||||
}
|
||||
|
||||
/// Draining iterator for `TinyVecDrain`
|
||||
|
||||
Reference in New Issue
Block a user