Skip to content

Commit b2c9c65

Browse files
author
bors-servo
authored
Auto merge of #173 - mbrubeck:std, r=emilio
Use no_std by default Since `alloc` became stable, the `std` feature is only needed to enable `impl Write for SmallVec`, which most users of this crate do not need. This patch replaces the enabled-by-default `std` feature with a disabled-by-default `write` feature. This decreases the chance that users of this crate will accidentally depend on libstd when they don't need it.
2 parents af73fbd + fffb185 commit b2c9c65

File tree

2 files changed

+28
-40
lines changed

2 files changed

+28
-40
lines changed

Cargo.toml

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ readme = "README.md"
1111
documentation = "https://doc.servo.org/smallvec/"
1212

1313
[features]
14-
std = []
14+
write = []
1515
union = []
16-
default = ["std"]
1716
specialization = []
1817
may_dangle = []
1918

lib.rs

+27-38
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
//! to the heap for larger allocations. This can be a useful optimization for improving cache
99
//! locality and reducing allocator traffic for workloads that fit within the inline buffer.
1010
//!
11-
//! ## no_std support
11+
//! ## `no_std` support
1212
//!
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`.
1616
//!
1717
//! To depend on `smallvec` without `libstd`, use `default-features = false` in the `smallvec`
1818
//! section of Cargo.toml to disable its `"std"` feature.
@@ -28,46 +28,43 @@
2828
//! To use this feature add `features = ["union"]` in the `smallvec` section of Cargo.toml.
2929
//! Note that this feature requires a nightly compiler (for now).
3030
31-
#![cfg_attr(not(feature = "std"), no_std)]
31+
#![no_std]
3232
#![cfg_attr(feature = "union", feature(untagged_unions))]
3333
#![cfg_attr(feature = "specialization", feature(specialization))]
3434
#![cfg_attr(feature = "may_dangle", feature(dropck_eyepatch))]
3535
#![deny(missing_docs)]
3636

37-
#[cfg(not(feature = "std"))]
3837
#[macro_use]
3938
extern crate alloc;
4039

41-
#[cfg(not(feature = "std"))]
42-
use alloc::vec::Vec;
40+
#[cfg(any(test, feature = "write"))]
41+
extern crate std;
4342

4443
#[cfg(feature = "serde")]
4544
extern crate serde;
4645

47-
#[cfg(not(feature = "std"))]
48-
mod std {
49-
pub use core::*;
50-
}
51-
5246
#[cfg(feature = "serde")]
5347
use serde::de::{Deserialize, Deserializer, SeqAccess, Visitor};
5448
#[cfg(feature = "serde")]
5549
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};
6458
#[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;
7168

7269
/// Creates a [`SmallVec`] containing the arguments.
7370
///
@@ -846,7 +843,7 @@ impl<A: Array> SmallVec<A> {
846843
}
847844

848845
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
850847
assert!(index + lower_size_bound >= index); // Protect against overflow
851848
self.reserve(lower_size_bound);
852849

@@ -1161,7 +1158,7 @@ where
11611158
let mut local_len = SetLenOnDrop::new(len_ptr);
11621159

11631160
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());
11651162
local_len.increment_len(1);
11661163
}
11671164
}
@@ -1219,7 +1216,7 @@ impl<A: Array> BorrowMut<[A::Item]> for SmallVec<A> {
12191216
}
12201217
}
12211218

1222-
#[cfg(feature = "std")]
1219+
#[cfg(feature = "write")]
12231220
impl<A: Array<Item = u8>> io::Write for SmallVec<A> {
12241221
#[inline]
12251222
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
@@ -1662,18 +1659,10 @@ mod tests {
16621659

16631660
use std::iter::FromIterator;
16641661

1665-
#[cfg(not(feature = "std"))]
16661662
use alloc::borrow::ToOwned;
1667-
#[cfg(not(feature = "std"))]
16681663
use alloc::boxed::Box;
1669-
#[cfg(not(feature = "std"))]
16701664
use alloc::rc::Rc;
1671-
#[cfg(not(feature = "std"))]
16721665
use alloc::vec::Vec;
1673-
#[cfg(feature = "std")]
1674-
use std::borrow::ToOwned;
1675-
#[cfg(feature = "std")]
1676-
use std::rc::Rc;
16771666

16781667
#[test]
16791668
pub fn test_zero() {

0 commit comments

Comments
 (0)