|
8 | 8 | //! to the heap for larger allocations. This can be a useful optimization for improving cache
|
9 | 9 | //! locality and reducing allocator traffic for workloads that fit within the inline buffer.
|
10 | 10 | //!
|
11 |
| -//! ## no_std support |
| 11 | +//! ## `no_std` support |
12 | 12 | //!
|
13 |
| -//! By default, `smallvec` depends on `libstd`. However, it can be configured to use the unstable |
14 |
| -//! `liballoc` API instead, for use on platforms that have `liballoc` but not `libstd`. This |
15 |
| -//! configuration is currently unstable and is not guaranteed to work on all versions of Rust. |
| 13 | +//! By default, `smallvec` does not depend on `std`. However, the optional |
| 14 | +//! `write` feature implements the `std::io::Write` trait for vectors of `u8`. |
| 15 | +//! When this feature is enabled, `smallvec` depends on `std`. |
16 | 16 | //!
|
17 | 17 | //! To depend on `smallvec` without `libstd`, use `default-features = false` in the `smallvec`
|
18 | 18 | //! section of Cargo.toml to disable its `"std"` feature.
|
|
28 | 28 | //! To use this feature add `features = ["union"]` in the `smallvec` section of Cargo.toml.
|
29 | 29 | //! Note that this feature requires a nightly compiler (for now).
|
30 | 30 |
|
31 |
| -#![cfg_attr(not(feature = "std"), no_std)] |
| 31 | +#![no_std] |
32 | 32 | #![cfg_attr(feature = "union", feature(untagged_unions))]
|
33 | 33 | #![cfg_attr(feature = "specialization", feature(specialization))]
|
34 | 34 | #![cfg_attr(feature = "may_dangle", feature(dropck_eyepatch))]
|
35 | 35 | #![deny(missing_docs)]
|
36 | 36 |
|
37 |
| -#[cfg(not(feature = "std"))] |
38 | 37 | #[macro_use]
|
39 | 38 | extern crate alloc;
|
40 | 39 |
|
41 |
| -#[cfg(not(feature = "std"))] |
42 |
| -use alloc::vec::Vec; |
| 40 | +#[cfg(any(test, feature = "write"))] |
| 41 | +extern crate std; |
43 | 42 |
|
44 | 43 | #[cfg(feature = "serde")]
|
45 | 44 | extern crate serde;
|
46 | 45 |
|
47 |
| -#[cfg(not(feature = "std"))] |
48 |
| -mod std { |
49 |
| - pub use core::*; |
50 |
| -} |
51 |
| - |
52 | 46 | #[cfg(feature = "serde")]
|
53 | 47 | use serde::de::{Deserialize, Deserializer, SeqAccess, Visitor};
|
54 | 48 | #[cfg(feature = "serde")]
|
55 | 49 | use serde::ser::{Serialize, SerializeSeq, Serializer};
|
56 |
| -use std::borrow::{Borrow, BorrowMut}; |
57 |
| -use std::cmp; |
58 |
| -use std::fmt; |
59 |
| -use std::hash::{Hash, Hasher}; |
60 |
| -use std::hint::unreachable_unchecked; |
61 |
| -#[cfg(feature = "std")] |
62 |
| -use std::io; |
63 |
| -use std::iter::{repeat, FromIterator, FusedIterator, IntoIterator}; |
| 50 | + |
| 51 | +use alloc::vec::Vec; |
| 52 | +use core::borrow::{Borrow, BorrowMut}; |
| 53 | +use core::cmp; |
| 54 | +use core::fmt; |
| 55 | +use core::hash::{Hash, Hasher}; |
| 56 | +use core::hint::unreachable_unchecked; |
| 57 | +use core::iter::{repeat, FromIterator, FusedIterator, IntoIterator}; |
64 | 58 | #[cfg(feature = "serde")]
|
65 |
| -use std::marker::PhantomData; |
66 |
| -use std::mem; |
67 |
| -use std::mem::MaybeUninit; |
68 |
| -use std::ops::{self, Bound, RangeBounds}; |
69 |
| -use std::ptr::{self, NonNull}; |
70 |
| -use std::slice::{self, SliceIndex}; |
| 59 | +use core::marker::PhantomData; |
| 60 | +use core::mem; |
| 61 | +use core::mem::MaybeUninit; |
| 62 | +use core::ops::{self, Bound, RangeBounds}; |
| 63 | +use core::ptr::{self, NonNull}; |
| 64 | +use core::slice::{self, SliceIndex}; |
| 65 | + |
| 66 | +#[cfg(feature = "write")] |
| 67 | +use std::io; |
71 | 68 |
|
72 | 69 | /// Creates a [`SmallVec`] containing the arguments.
|
73 | 70 | ///
|
@@ -846,7 +843,7 @@ impl<A: Array> SmallVec<A> {
|
846 | 843 | }
|
847 | 844 |
|
848 | 845 | let (lower_size_bound, _) = iter.size_hint();
|
849 |
| - assert!(lower_size_bound <= std::isize::MAX as usize); // Ensure offset is indexable |
| 846 | + assert!(lower_size_bound <= core::isize::MAX as usize); // Ensure offset is indexable |
850 | 847 | assert!(index + lower_size_bound >= index); // Protect against overflow
|
851 | 848 | self.reserve(lower_size_bound);
|
852 | 849 |
|
@@ -1161,7 +1158,7 @@ where
|
1161 | 1158 | let mut local_len = SetLenOnDrop::new(len_ptr);
|
1162 | 1159 |
|
1163 | 1160 | for i in 0..n as isize {
|
1164 |
| - ::std::ptr::write(ptr.offset(i), elem.clone()); |
| 1161 | + ::core::ptr::write(ptr.offset(i), elem.clone()); |
1165 | 1162 | local_len.increment_len(1);
|
1166 | 1163 | }
|
1167 | 1164 | }
|
@@ -1219,7 +1216,7 @@ impl<A: Array> BorrowMut<[A::Item]> for SmallVec<A> {
|
1219 | 1216 | }
|
1220 | 1217 | }
|
1221 | 1218 |
|
1222 |
| -#[cfg(feature = "std")] |
| 1219 | +#[cfg(feature = "write")] |
1223 | 1220 | impl<A: Array<Item = u8>> io::Write for SmallVec<A> {
|
1224 | 1221 | #[inline]
|
1225 | 1222 | fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
@@ -1662,18 +1659,10 @@ mod tests {
|
1662 | 1659 |
|
1663 | 1660 | use std::iter::FromIterator;
|
1664 | 1661 |
|
1665 |
| - #[cfg(not(feature = "std"))] |
1666 | 1662 | use alloc::borrow::ToOwned;
|
1667 |
| - #[cfg(not(feature = "std"))] |
1668 | 1663 | use alloc::boxed::Box;
|
1669 |
| - #[cfg(not(feature = "std"))] |
1670 | 1664 | use alloc::rc::Rc;
|
1671 |
| - #[cfg(not(feature = "std"))] |
1672 | 1665 | use alloc::vec::Vec;
|
1673 |
| - #[cfg(feature = "std")] |
1674 |
| - use std::borrow::ToOwned; |
1675 |
| - #[cfg(feature = "std")] |
1676 |
| - use std::rc::Rc; |
1677 | 1666 |
|
1678 | 1667 | #[test]
|
1679 | 1668 | pub fn test_zero() {
|
|
0 commit comments