aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff Vander Stoep <jeffv@google.com>2022-12-19 15:41:32 +0000
committerAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>2022-12-19 15:41:32 +0000
commit1cef3f599ca84f2a2fcae1654c594ee7fa9bfb30 (patch)
tree856cf5e90aafdd7731447d325bc2aa2339f37451
parentfe2507000dadd52b96daec114ebb7c5c1b432dc0 (diff)
parent31d99591ee3ddb7d611783586d71969bf86ab11d (diff)
downloadtinyvec-1cef3f599ca84f2a2fcae1654c594ee7fa9bfb30.tar.gz
Upgrade tinyvec to 1.6.0 am: ceb662cbb2 am: 31d99591ee
Original change: https://android-review.googlesource.com/c/platform/external/rust/crates/tinyvec/+/2362484 Change-Id: I791c8d0c3d15874d2e7ab819d02f16e108e0b7b3 Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
-rw-r--r--.cargo_vcs_info.json2
-rw-r--r--Android.bp4
-rw-r--r--CHANGELOG.md8
-rw-r--r--Cargo.toml54
-rw-r--r--Cargo.toml.orig8
-rw-r--r--METADATA12
-rw-r--r--src/arrayvec.rs63
-rw-r--r--src/tinyvec.rs126
-rw-r--r--tests/tinyvec.rs57
9 files changed, 315 insertions, 19 deletions
diff --git a/.cargo_vcs_info.json b/.cargo_vcs_info.json
index 6772581..fcfc522 100644
--- a/.cargo_vcs_info.json
+++ b/.cargo_vcs_info.json
@@ -1,6 +1,6 @@
{
"git": {
- "sha1": "1ef53f632b9cb6f2e7ff603586f4472317b9a25c"
+ "sha1": "6e326cc48208229d1f28abf74a3e06944c8f7bec"
},
"path_in_vcs": ""
} \ No newline at end of file
diff --git a/Android.bp b/Android.bp
index 65d78b2..d8574c7 100644
--- a/Android.bp
+++ b/Android.bp
@@ -44,7 +44,7 @@ rust_library {
host_supported: true,
crate_name: "tinyvec",
cargo_env_compat: true,
- cargo_pkg_version: "1.5.1",
+ cargo_pkg_version: "1.6.0",
srcs: ["src/lib.rs"],
edition: "2018",
features: [
@@ -67,7 +67,7 @@ rust_defaults {
name: "tinyvec_test_defaults",
crate_name: "tinyvec",
cargo_env_compat: true,
- cargo_pkg_version: "1.5.1",
+ cargo_pkg_version: "1.6.0",
test_suites: ["general-tests"],
auto_gen_config: true,
edition: "2018",
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 93ef808..6f7d2c0 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,13 @@
# Changelog
+## 1.6.0
+
+* [i509VCB](https://github.com/i509VCB) added the `try_` functions for fallable reallocation.
+ [pr 158](https://github.com/Lokathor/tinyvec/pull/158)
+* [ajtribick](https://github.com/ajtribick) added more error impls to `TryFromSliceError`.
+ [pr 160](https://github.com/Lokathor/tinyvec/pull/160)
+* The `std` feature now automatically enables the `alloc` feature as well.
+
## 1.5.1
* [madsmtm](https://github.com/madsmtm) fixed an error with the `alloc` feature on very old rustc versions.
diff --git a/Cargo.toml b/Cargo.toml
index 24dfa0d..6c9c35e 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -12,19 +12,46 @@
[package]
edition = "2018"
name = "tinyvec"
-version = "1.5.1"
+version = "1.6.0"
authors = ["Lokathor <zefria@gmail.com>"]
description = "`tinyvec` provides 100% safe vec-like data structures."
-keywords = ["vec", "no_std", "no-std"]
-categories = ["data-structures", "no-std"]
+readme = "README.md"
+keywords = [
+ "vec",
+ "no_std",
+ "no-std",
+]
+categories = [
+ "data-structures",
+ "no-std",
+]
license = "Zlib OR Apache-2.0 OR MIT"
repository = "https://github.com/Lokathor/tinyvec"
+
[package.metadata.docs.rs]
-features = ["alloc", "std", "grab_spare_slice", "rustc_1_40", "rustc_1_55", "serde"]
-rustdoc-args = ["--cfg", "docs_rs"]
+features = [
+ "alloc",
+ "std",
+ "grab_spare_slice",
+ "rustc_1_40",
+ "rustc_1_55",
+ "serde",
+]
+rustdoc-args = [
+ "--cfg",
+ "docs_rs",
+]
[package.metadata.playground]
-features = ["alloc", "std", "grab_spare_slice", "rustc_1_40", "rustc_1_55", "serde"]
+features = [
+ "alloc",
+ "std",
+ "grab_spare_slice",
+ "rustc_1_40",
+ "rustc_1_55",
+ "serde",
+]
+
[profile.bench]
debug = 2
@@ -33,7 +60,10 @@ opt-level = 3
[[test]]
name = "tinyvec"
-required-features = ["alloc", "std"]
+required-features = [
+ "alloc",
+ "std",
+]
[[bench]]
name = "macros"
@@ -43,7 +73,11 @@ required-features = ["alloc"]
[[bench]]
name = "smallvec"
harness = false
-required-features = ["alloc", "real_blackbox"]
+required-features = [
+ "alloc",
+ "real_blackbox",
+]
+
[dependencies.arbitrary]
version = "1"
optional = true
@@ -56,6 +90,7 @@ default-features = false
[dependencies.tinyvec_macros]
version = "0.1"
optional = true
+
[dev-dependencies.criterion]
version = "0.3.0"
@@ -74,4 +109,5 @@ nightly_slice_partition_dedup = []
real_blackbox = ["criterion/real_blackbox"]
rustc_1_40 = []
rustc_1_55 = ["rustc_1_40"]
-std = []
+rustc_1_57 = ["rustc_1_55"]
+std = ["alloc"]
diff --git a/Cargo.toml.orig b/Cargo.toml.orig
index 28a5874..b18ea9e 100644
--- a/Cargo.toml.orig
+++ b/Cargo.toml.orig
@@ -1,7 +1,7 @@
[package]
name = "tinyvec"
description = "`tinyvec` provides 100% safe vec-like data structures."
-version = "1.5.1"
+version = "1.6.0"
authors = ["Lokathor <zefria@gmail.com>"]
edition = "2018"
license = "Zlib OR Apache-2.0 OR MIT"
@@ -23,7 +23,7 @@ default = []
alloc = ["tinyvec_macros"]
# Provide things that require Rust's `std` module
-std = []
+std = ["alloc"]
# (not part of Vec!) Extra methods to let you grab the slice of memory after the
# "active" portion of an `ArrayVec` or `SliceVec`.
@@ -38,6 +38,10 @@ rustc_1_40 = []
# use const generics to implement Array for all array lengths
rustc_1_55 = ["rustc_1_40"]
+# features that require rustc 1.57
+# add try_reserve functions to types that heap allocate.
+rustc_1_57 = ["rustc_1_55"]
+
# allow use of nightly feature `slice_partition_dedup`,
# will become useless once that is stabilized:
# https://github.com/rust-lang/rust/issues/54279
diff --git a/METADATA b/METADATA
index a632067..82a6d92 100644
--- a/METADATA
+++ b/METADATA
@@ -1,3 +1,7 @@
+# This project was upgraded with external_updater.
+# Usage: tools/external_updater/updater.sh update rust/crates/tinyvec
+# For more info, check https://cs.android.com/android/platform/superproject/+/master:tools/external_updater/README.md
+
name: "tinyvec"
description: "`tinyvec` provides 100% safe vec-like data structures."
third_party {
@@ -7,13 +11,13 @@ third_party {
}
url {
type: ARCHIVE
- value: "https://static.crates.io/crates/tinyvec/tinyvec-1.5.1.crate"
+ value: "https://static.crates.io/crates/tinyvec/tinyvec-1.6.0.crate"
}
- version: "1.5.1"
+ version: "1.6.0"
license_type: NOTICE
last_upgrade_date {
year: 2022
- month: 3
- day: 1
+ month: 12
+ day: 19
}
}
diff --git a/src/arrayvec.rs b/src/arrayvec.rs
index 7cf1bfc..9cfe58d 100644
--- a/src/arrayvec.rs
+++ b/src/arrayvec.rs
@@ -1271,7 +1271,7 @@ impl<A: Array> From<A> for ArrayVec<A> {
.as_slice()
.len()
.try_into()
- .expect("ArrayVec::from> lenght must be in range 0..=u16::MAX");
+ .expect("ArrayVec::from> length must be in range 0..=u16::MAX");
Self { len, data }
}
}
@@ -1281,6 +1281,15 @@ impl<A: Array> From<A> for ArrayVec<A> {
#[derive(Debug, Copy, Clone)]
pub struct TryFromSliceError(());
+impl core::fmt::Display for TryFromSliceError {
+ fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
+ f.write_str("could not convert slice to ArrayVec")
+ }
+}
+
+#[cfg(feature = "std")]
+impl std::error::Error for TryFromSliceError {}
+
impl<T, A> TryFrom<&'_ [T]> for ArrayVec<A>
where
T: Clone + Default,
@@ -1744,6 +1753,9 @@ where
#[cfg(feature = "alloc")]
use alloc::vec::Vec;
+#[cfg(all(feature = "alloc", feature = "rustc_1_57"))]
+use alloc::collections::TryReserveError;
+
#[cfg(feature = "alloc")]
impl<A: Array> ArrayVec<A> {
/// Drains all elements to a Vec, but reserves additional space
@@ -1763,6 +1775,34 @@ impl<A: Array> ArrayVec<A> {
return v;
}
+ /// Tries to drain all elements to a Vec, but reserves additional space.
+ ///
+ /// # Errors
+ ///
+ /// If the allocator reports a failure, then an error is returned.
+ ///
+ /// ```
+ /// # use tinyvec::*;
+ /// let mut av = array_vec!([i32; 7] => 1, 2, 3);
+ /// let v = av.try_drain_to_vec_and_reserve(10);
+ /// assert!(matches!(v, Ok(_)));
+ /// let v = v.unwrap();
+ /// assert_eq!(v, &[1, 2, 3]);
+ /// assert_eq!(v.capacity(), 13);
+ /// ```
+ #[cfg(feature = "rustc_1_57")]
+ pub fn try_drain_to_vec_and_reserve(
+ &mut self, n: usize,
+ ) -> Result<Vec<A::Item>, TryReserveError> {
+ let cap = n + self.len();
+ let mut v = Vec::new();
+ v.try_reserve(cap)?;
+ let iter = self.iter_mut().map(take);
+ v.extend(iter);
+ self.set_len(0);
+ return Ok(v);
+ }
+
/// Drains all elements to a Vec
/// ```
/// # use tinyvec::*;
@@ -1774,6 +1814,27 @@ impl<A: Array> ArrayVec<A> {
pub fn drain_to_vec(&mut self) -> Vec<A::Item> {
self.drain_to_vec_and_reserve(0)
}
+
+ /// Tries to drain all elements to a Vec.
+ ///
+ /// # Errors
+ ///
+ /// If the allocator reports a failure, then an error is returned.
+ ///
+ /// ```
+ /// # use tinyvec::*;
+ /// let mut av = array_vec!([i32; 7] => 1, 2, 3);
+ /// let v = av.try_drain_to_vec();
+ /// assert!(matches!(v, Ok(_)));
+ /// let v = v.unwrap();
+ /// assert_eq!(v, &[1, 2, 3]);
+ /// // Vec may reserve more than necessary in order to prevent more future allocations.
+ /// assert!(v.capacity() >= 3);
+ /// ```
+ #[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)
+ }
}
#[cfg(feature = "serde")]
diff --git a/src/tinyvec.rs b/src/tinyvec.rs
index 1e11a47..a5e1c15 100644
--- a/src/tinyvec.rs
+++ b/src/tinyvec.rs
@@ -6,6 +6,9 @@ use alloc::vec::{self, Vec};
use core::convert::TryFrom;
use tinyvec_macros::impl_mirrored;
+#[cfg(feature = "rustc_1_57")]
+use alloc::collections::TryReserveError;
+
#[cfg(feature = "serde")]
use core::marker::PhantomData;
#[cfg(feature = "serde")]
@@ -300,6 +303,32 @@ impl<A: Array> TinyVec<A> {
*self = TinyVec::Heap(v);
}
+ /// Tries to move the content of the TinyVec to the heap, if it's inline.
+ ///
+ /// # Errors
+ ///
+ /// If the allocator reports a failure, then an error is returned and the
+ /// content is kept on the stack.
+ ///
+ /// ```rust
+ /// use tinyvec::*;
+ /// let mut tv = tiny_vec!([i32; 4] => 1, 2, 3);
+ /// assert!(tv.is_inline());
+ /// assert_eq!(Ok(()), tv.try_move_to_the_heap());
+ /// assert!(tv.is_heap());
+ /// ```
+ #[cfg(feature = "rustc_1_57")]
+ pub fn try_move_to_the_heap(&mut self) -> Result<(), TryReserveError> {
+ let arr = match self {
+ TinyVec::Heap(_) => return Ok(()),
+ TinyVec::Inline(a) => a,
+ };
+
+ let v = arr.try_drain_to_vec()?;
+ *self = TinyVec::Heap(v);
+ return Ok(());
+ }
+
/// If TinyVec is inline, moves the content of it to the heap.
/// Also reserves additional space.
/// ```rust
@@ -320,6 +349,35 @@ impl<A: Array> TinyVec<A> {
*self = TinyVec::Heap(v);
}
+ /// If TinyVec is inline, try to move the content of it to the heap.
+ /// Also reserves additional space.
+ ///
+ /// # Errors
+ ///
+ /// If the allocator reports a failure, then an error is returned.
+ ///
+ /// ```rust
+ /// use tinyvec::*;
+ /// let mut tv = tiny_vec!([i32; 4] => 1, 2, 3);
+ /// assert!(tv.is_inline());
+ /// assert_eq!(Ok(()), tv.try_move_to_the_heap_and_reserve(32));
+ /// assert!(tv.is_heap());
+ /// assert!(tv.capacity() >= 35);
+ /// ```
+ #[cfg(feature = "rustc_1_57")]
+ pub fn try_move_to_the_heap_and_reserve(
+ &mut self, n: usize,
+ ) -> Result<(), TryReserveError> {
+ let arr = match self {
+ TinyVec::Heap(h) => return h.try_reserve(n),
+ TinyVec::Inline(a) => a,
+ };
+
+ let v = arr.try_drain_to_vec_and_reserve(n)?;
+ *self = TinyVec::Heap(v);
+ return Ok(());
+ }
+
/// Reserves additional space.
/// Moves to the heap if array can't hold `n` more items
/// ```rust
@@ -345,6 +403,37 @@ impl<A: Array> TinyVec<A> {
return;
}
+ /// Tries to reserve additional space.
+ /// Moves to the heap if array can't hold `n` more items.
+ ///
+ /// # Errors
+ ///
+ /// If the allocator reports a failure, then an error is returned.
+ ///
+ /// ```rust
+ /// use tinyvec::*;
+ /// let mut tv = tiny_vec!([i32; 4] => 1, 2, 3, 4);
+ /// assert!(tv.is_inline());
+ /// assert_eq!(Ok(()), tv.try_reserve(1));
+ /// assert!(tv.is_heap());
+ /// assert!(tv.capacity() >= 5);
+ /// ```
+ #[cfg(feature = "rustc_1_57")]
+ pub fn try_reserve(&mut self, n: usize) -> Result<(), TryReserveError> {
+ let arr = match self {
+ TinyVec::Heap(h) => return h.try_reserve(n),
+ TinyVec::Inline(a) => a,
+ };
+
+ if n > arr.capacity() - arr.len() {
+ let v = arr.try_drain_to_vec_and_reserve(n)?;
+ *self = TinyVec::Heap(v);
+ }
+
+ /* In this place array has enough place, so no work is needed more */
+ return Ok(());
+ }
+
/// Reserves additional space.
/// Moves to the heap if array can't hold `n` more items
///
@@ -377,6 +466,43 @@ impl<A: Array> TinyVec<A> {
return;
}
+ /// Tries to reserve additional space.
+ /// Moves to the heap if array can't hold `n` more items
+ ///
+ /// # Errors
+ ///
+ /// If the allocator reports a failure, then an error is returned.
+ ///
+ /// From [Vec::try_reserve_exact](https://doc.rust-lang.org/std/vec/struct.Vec.html#method.try_reserve_exact)
+ /// ```text
+ /// Note that the allocator may give the collection more space than it requests.
+ /// Therefore, capacity can not be relied upon to be precisely minimal.
+ /// Prefer `reserve` if future insertions are expected.
+ /// ```
+ /// ```rust
+ /// use tinyvec::*;
+ /// let mut tv = tiny_vec!([i32; 4] => 1, 2, 3, 4);
+ /// assert!(tv.is_inline());
+ /// assert_eq!(Ok(()), tv.try_reserve_exact(1));
+ /// assert!(tv.is_heap());
+ /// assert!(tv.capacity() >= 5);
+ /// ```
+ #[cfg(feature = "rustc_1_57")]
+ pub fn try_reserve_exact(&mut self, n: usize) -> Result<(), TryReserveError> {
+ let arr = match self {
+ TinyVec::Heap(h) => return h.try_reserve_exact(n),
+ TinyVec::Inline(a) => a,
+ };
+
+ if n > arr.capacity() - arr.len() {
+ let v = arr.try_drain_to_vec_and_reserve(n)?;
+ *self = TinyVec::Heap(v);
+ }
+
+ /* In this place array has enough place, so no work is needed more */
+ return Ok(());
+ }
+
/// Makes a new TinyVec with _at least_ the given capacity.
///
/// If the requested capacity is less than or equal to the array capacity you
diff --git a/tests/tinyvec.rs b/tests/tinyvec.rs
index ab4d9bd..658462a 100644
--- a/tests/tinyvec.rs
+++ b/tests/tinyvec.rs
@@ -304,6 +304,22 @@ fn TinyVec_reserve() {
assert!(tv.capacity() >= 10);
}
+#[cfg(feature = "rustc_1_57")]
+#[test]
+fn TinyVec_try_reserve() {
+ let mut tv: TinyVec<[i32; 4]> = Default::default();
+ assert_eq!(tv.capacity(), 4);
+ tv.extend_from_slice(&[1, 2]);
+ assert_eq!(tv.capacity(), 4);
+ assert!(tv.try_reserve(2).is_ok());
+ assert_eq!(tv.capacity(), 4);
+ assert!(tv.try_reserve(4).is_ok());
+ assert!(tv.capacity() >= 6);
+ tv.extend_from_slice(&[3, 4, 5, 6]);
+ assert!(tv.try_reserve(4).is_ok());
+ assert!(tv.capacity() >= 10);
+}
+
#[test]
fn TinyVec_reserve_exact() {
let mut tv: TinyVec<[i32; 4]> = Default::default();
@@ -320,6 +336,23 @@ fn TinyVec_reserve_exact() {
assert!(tv.capacity() >= 10);
}
+#[cfg(feature = "rustc_1_57")]
+#[test]
+fn TinyVec_try_reserve_exact() {
+ let mut tv: TinyVec<[i32; 4]> = Default::default();
+ assert_eq!(tv.capacity(), 4);
+
+ tv.extend_from_slice(&[1, 2]);
+ assert_eq!(tv.capacity(), 4);
+ assert!(tv.try_reserve_exact(2).is_ok());
+ assert_eq!(tv.capacity(), 4);
+ assert!(tv.try_reserve_exact(4).is_ok());
+ assert!(tv.capacity() >= 6);
+ tv.extend_from_slice(&[3, 4, 5, 6]);
+ assert!(tv.try_reserve_exact(4).is_ok());
+ assert!(tv.capacity() >= 10);
+}
+
#[test]
fn TinyVec_move_to_heap_and_shrink() {
let mut tv: TinyVec<[i32; 4]> = Default::default();
@@ -341,6 +374,30 @@ fn TinyVec_move_to_heap_and_shrink() {
assert_eq!(tv.as_slice(), [1, 2, 3, 4]);
}
+#[cfg(feature = "rustc_1_57")]
+#[test]
+fn TinyVec_try_move_to_heap_and_shrink() {
+ let mut tv: TinyVec<[i32; 4]> = Default::default();
+ assert!(tv.is_inline());
+ assert!(tv.try_move_to_the_heap().is_ok());
+ assert!(tv.is_heap());
+ assert_eq!(tv.capacity(), 0);
+
+ assert!(tv.try_reserve_exact(1).is_ok());
+ assert_eq!(tv.capacity(), 1);
+ tv.push(1);
+ tv.shrink_to_fit();
+ assert!(tv.is_inline());
+ assert_eq!(tv.capacity(), 4);
+
+ assert!(tv.try_move_to_the_heap_and_reserve(3).is_ok());
+ assert!(tv.is_heap());
+ assert_eq!(tv.capacity(), 4);
+ tv.extend(2..=4);
+ assert_eq!(tv.capacity(), 4);
+ assert_eq!(tv.as_slice(), [1, 2, 3, 4]);
+}
+
#[cfg(feature = "serde")]
#[test]
fn TinyVec_ser_de_empty() {