Skip to content

Commit

Permalink
feat: replace write feature with std feature (#356)
Browse files Browse the repository at this point in the history
* feat: replace `write` feature with `std` feature

This also implements `std::error::Error` on `CollectionAllocErr`

* docs: describe `std` feature
  • Loading branch information
JarvisCraft authored May 14, 2024
1 parent c83d633 commit 1dd57a4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ readme = "README.md"
documentation = "https://docs.rs/smallvec/"

[features]
write = []
std = []
specialization = []
may_dangle = []
extract_if = []
Expand Down
28 changes: 18 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,20 @@
//!
//! ## Optional features
//!
//! ### `serde`
//! ### `std`
//!
//! When this optional dependency is enabled, `SmallVec` implements the `serde::Serialize` and
//! `serde::Deserialize` traits.
//! When this feature is enabled, traits available from `std` are implemented:
//!
//! ### `write`
//! * `SmallVec<u8, _>` implements the [`std::io::Write`] trait.
//! * [`CollectionAllocErr`] implements [`std::error::Error`].
//!
//! When this feature is enabled, `SmallVec<u8, _>` implements the `std::io::Write` trait.
//! This feature is not compatible with `#![no_std]` programs.
//!
//! ### `serde`
//!
//! When this optional dependency is enabled, `SmallVec` implements the `serde::Serialize` and
//! `serde::Deserialize` traits.
//!
//! ### `extract_if`
//!
//! **This feature is unstable.** It may change to match the unstable `extract_if` method in libstd.
Expand Down Expand Up @@ -62,7 +66,7 @@
#[doc(hidden)]
pub extern crate alloc;

#[cfg(any(test, feature = "write"))]
#[cfg(any(test, feature = "std"))]
extern crate std;

#[cfg(test)]
Expand Down Expand Up @@ -93,7 +97,7 @@ use serde::{
de::{Deserialize, Deserializer, SeqAccess, Visitor},
ser::{Serialize, SerializeSeq, Serializer},
};
#[cfg(feature = "write")]
#[cfg(feature = "std")]
use std::io;

/// Error type for APIs with fallible heap allocation
Expand All @@ -113,6 +117,10 @@ impl core::fmt::Display for CollectionAllocErr {
}
}

#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl std::error::Error for CollectionAllocErr {}

/// Either a stack array with `length <= N` or a heap array
/// whose pointer and capacity are stored here.
///
Expand Down Expand Up @@ -797,7 +805,7 @@ impl<T, const N: usize> SmallVec<T, N> {
/// the elements `[0, at)` with its previous capacity unchanged.
///
/// - If you want to take ownership of the entire contents and capacity of
/// the vector, see [`mem::take`] or [`mem::replace`].
/// the vector, see [`core::mem::take`] or [`core::mem::replace`].
/// - If you don't need the returned vector at all, see [`SmallVec::truncate`].
/// - If you want to take ownership of an arbitrary subslice, or you don't
/// necessarily want to store the removed items in a vector, see [`SmallVec::drain`].
Expand Down Expand Up @@ -2145,8 +2153,8 @@ where
}
}

#[cfg(feature = "write")]
#[cfg_attr(docsrs, doc(cfg(feature = "write")))]
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl<const N: usize> io::Write for SmallVec<u8, N> {
#[inline]
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
Expand Down
2 changes: 1 addition & 1 deletion src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,7 @@ fn test_resize() {
assert_eq!(v[..], [1, 0][..]);
}

#[cfg(feature = "write")]
#[cfg(feature = "std")]
#[test]
fn test_write() {
use std::io::Write;
Expand Down

0 comments on commit 1dd57a4

Please sign in to comment.