Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for no_std compilation. #74

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,13 @@ derive_arbitrary = { version = "1.0.0", path = "./derive", optional = true }
[dev-dependencies]

[features]
# Provide support for std types by default.
default = ["std"]
# Turn this feature on to enable support for `#[derive(Arbitrary)]`.
derive = ["derive_arbitrary"]
# Whether or not to require the standard library. Turning off this feature
# makes Arbitrary work with no_std crates.
std = []

[[example]]
name = "derive_enum"
Expand Down
8 changes: 6 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::{error, fmt};
#[cfg(feature = "std")]
use std::error;

use core::fmt;

/// An enumeration of buffer creation errors
#[derive(Debug, Clone, Copy)]
Expand Down Expand Up @@ -32,9 +35,10 @@ impl fmt::Display for Error {
}
}

#[cfg(feature = "std")]
impl error::Error for Error {}

/// A `Result` with the error type fixed as `arbitrary::Error`.
///
/// Either an `Ok(T)` or `Err(arbitrary::Error)`.
pub type Result<T> = std::result::Result<T, Error>;
pub type Result<T> = core::result::Result<T, Error>;
51 changes: 45 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
//! [`Arbitrary`](./trait.Arbitrary.html) trait's documentation for details on
//! automatically deriving, implementing, and/or using the trait.

#![cfg_attr(not(feature = "std"), no_std)]
#![deny(bad_style)]
#![deny(missing_docs)]
#![deny(future_incompatible)]
Expand All @@ -40,12 +41,25 @@ use core::mem;
use core::ops::{Range, RangeBounds, RangeFrom, RangeInclusive, RangeTo, RangeToInclusive};
use core::str;
use core::time::Duration;

#[cfg(not(feature = "std"))]
extern crate alloc;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

#[cfg(feature = "std")]
use std::borrow::{Cow, ToOwned};
#[cfg(feature = "std")]
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, LinkedList, VecDeque};
#[cfg(feature = "std")]
use std::ffi::{CString, OsString};
#[cfg(feature = "std")]
use std::path::PathBuf;
#[cfg(feature = "std")]
use std::rc::Rc;
#[cfg(feature = "std")]
use std::sync::atomic::{AtomicBool, AtomicIsize, AtomicUsize};
#[cfg(feature = "std")]
use std::sync::{Arc, Mutex};

/// Generate arbitrary structured values from raw, unstructured data.
Expand Down Expand Up @@ -75,11 +89,13 @@ use std::sync::{Arc, Mutex};
/// use arbitrary::Arbitrary;
/// use std::collections::HashSet;
///
/// # #[cfg(feature = "std")]
/// #[derive(Arbitrary)]
/// pub struct AddressBook {
/// friends: HashSet<Friend>,
/// }
///
/// # #[cfg(feature = "std")]
/// #[derive(Arbitrary, Hash, Eq, PartialEq)]
/// pub enum Friend {
/// Buddy { name: String },
Expand Down Expand Up @@ -344,7 +360,7 @@ impl_arbitrary_for_floats! {

impl<'a> Arbitrary<'a> for char {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
use std::char;
use core::char;
const CHAR_END: u32 = 0x0011_000;
// The size of the surrogate blocks
const SURROGATES_START: u32 = 0xD800;
Expand All @@ -365,6 +381,7 @@ impl<'a> Arbitrary<'a> for char {
}
}

#[cfg(feature = "std")]
impl<'a> Arbitrary<'a> for AtomicBool {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
Arbitrary::arbitrary(u).map(Self::new)
Expand All @@ -376,6 +393,7 @@ impl<'a> Arbitrary<'a> for AtomicBool {
}
}

#[cfg(feature = "std")]
impl<'a> Arbitrary<'a> for AtomicIsize {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
Arbitrary::arbitrary(u).map(Self::new)
Expand All @@ -387,6 +405,7 @@ impl<'a> Arbitrary<'a> for AtomicIsize {
}
}

#[cfg(feature = "std")]
impl<'a> Arbitrary<'a> for AtomicUsize {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
Arbitrary::arbitrary(u).map(Self::new)
Expand Down Expand Up @@ -521,7 +540,7 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Option<A> {
}
}

impl<'a, A: Arbitrary<'a>, B: Arbitrary<'a>> Arbitrary<'a> for std::result::Result<A, B> {
impl<'a, A: Arbitrary<'a>, B: Arbitrary<'a>> Arbitrary<'a> for core::result::Result<A, B> {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
Ok(if <bool as Arbitrary<'a>>::arbitrary(u)? {
Ok(<A as Arbitrary>::arbitrary(u)?)
Expand Down Expand Up @@ -657,6 +676,7 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Vec<A> {
}
}

#[cfg(feature = "std")]
impl<'a, K: Arbitrary<'a> + Ord, V: Arbitrary<'a>> Arbitrary<'a> for BTreeMap<K, V> {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
u.arbitrary_iter()?.collect()
Expand All @@ -672,6 +692,7 @@ impl<'a, K: Arbitrary<'a> + Ord, V: Arbitrary<'a>> Arbitrary<'a> for BTreeMap<K,
}
}

#[cfg(feature = "std")]
impl<'a, A: Arbitrary<'a> + Ord> Arbitrary<'a> for BTreeSet<A> {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
u.arbitrary_iter()?.collect()
Expand All @@ -687,6 +708,7 @@ impl<'a, A: Arbitrary<'a> + Ord> Arbitrary<'a> for BTreeSet<A> {
}
}

#[cfg(feature = "std")]
impl<'a, A: Arbitrary<'a> + Ord> Arbitrary<'a> for BinaryHeap<A> {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
u.arbitrary_iter()?.collect()
Expand All @@ -702,6 +724,7 @@ impl<'a, A: Arbitrary<'a> + Ord> Arbitrary<'a> for BinaryHeap<A> {
}
}

#[cfg(feature = "std")]
impl<'a, K: Arbitrary<'a> + Eq + ::std::hash::Hash, V: Arbitrary<'a>> Arbitrary<'a>
for HashMap<K, V>
{
Expand All @@ -719,6 +742,7 @@ impl<'a, K: Arbitrary<'a> + Eq + ::std::hash::Hash, V: Arbitrary<'a>> Arbitrary<
}
}

#[cfg(feature = "std")]
impl<'a, A: Arbitrary<'a> + Eq + ::std::hash::Hash> Arbitrary<'a> for HashSet<A> {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
u.arbitrary_iter()?.collect()
Expand All @@ -734,6 +758,7 @@ impl<'a, A: Arbitrary<'a> + Eq + ::std::hash::Hash> Arbitrary<'a> for HashSet<A>
}
}

#[cfg(feature = "std")]
impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for LinkedList<A> {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
u.arbitrary_iter()?.collect()
Expand All @@ -749,6 +774,7 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for LinkedList<A> {
}
}

#[cfg(feature = "std")]
impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for VecDeque<A> {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
u.arbitrary_iter()?.collect()
Expand All @@ -764,6 +790,7 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for VecDeque<A> {
}
}

#[cfg(feature = "std")]
impl<'a, A> Arbitrary<'a> for Cow<'a, A>
where
A: ToOwned + ?Sized,
Expand Down Expand Up @@ -814,6 +841,7 @@ impl<'a> Arbitrary<'a> for &'a str {
}
}

#[cfg(feature = "std")]
impl<'a> Arbitrary<'a> for String {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
<&str as Arbitrary>::arbitrary(u).map(Into::into)
Expand All @@ -829,6 +857,7 @@ impl<'a> Arbitrary<'a> for String {
}
}

#[cfg(feature = "std")]
impl<'a> Arbitrary<'a> for CString {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
<Vec<u8> as Arbitrary>::arbitrary(u).map(|mut x| {
Expand All @@ -843,6 +872,7 @@ impl<'a> Arbitrary<'a> for CString {
}
}

#[cfg(feature = "std")]
impl<'a> Arbitrary<'a> for OsString {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
<String as Arbitrary>::arbitrary(u).map(From::from)
Expand All @@ -854,6 +884,7 @@ impl<'a> Arbitrary<'a> for OsString {
}
}

#[cfg(feature = "std")]
impl<'a> Arbitrary<'a> for PathBuf {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
<OsString as Arbitrary>::arbitrary(u).map(From::from)
Expand All @@ -865,6 +896,7 @@ impl<'a> Arbitrary<'a> for PathBuf {
}
}

#[cfg(feature = "std")]
impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Box<A> {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
Arbitrary::arbitrary(u).map(Self::new)
Expand All @@ -876,6 +908,7 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Box<A> {
}
}

#[cfg(feature = "std")]
impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Box<[A]> {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
<Vec<A> as Arbitrary>::arbitrary(u).map(|x| x.into_boxed_slice())
Expand All @@ -887,6 +920,7 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Box<[A]> {
}
}

#[cfg(feature = "std")]
impl<'a> Arbitrary<'a> for Box<str> {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
<String as Arbitrary>::arbitrary(u).map(|x| x.into_boxed_str())
Expand All @@ -911,6 +945,7 @@ impl<'a> Arbitrary<'a> for Box<str> {
// }
// }

#[cfg(feature = "std")]
impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Arc<A> {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
Arbitrary::arbitrary(u).map(Self::new)
Expand All @@ -922,6 +957,7 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Arc<A> {
}
}

#[cfg(feature = "std")]
impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Rc<A> {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
Arbitrary::arbitrary(u).map(Self::new)
Expand Down Expand Up @@ -966,6 +1002,7 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for UnsafeCell<A> {
}
}

#[cfg(feature = "std")]
impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Mutex<A> {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
Arbitrary::arbitrary(u).map(Self::new)
Expand All @@ -988,9 +1025,9 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for iter::Empty<A> {
}
}

impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for ::std::marker::PhantomData<A> {
impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for core::marker::PhantomData<A> {
fn arbitrary(_: &mut Unstructured<'a>) -> Result<Self> {
Ok(::std::marker::PhantomData)
Ok(core::marker::PhantomData)
}

#[inline]
Expand All @@ -999,9 +1036,9 @@ impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for ::std::marker::PhantomData<A> {
}
}

impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for ::std::num::Wrapping<A> {
impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for core::num::Wrapping<A> {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
Arbitrary::arbitrary(u).map(::std::num::Wrapping)
Arbitrary::arbitrary(u).map(core::num::Wrapping)
}

#[inline]
Expand Down Expand Up @@ -1067,6 +1104,7 @@ mod test {
Vec::<u32>::arbitrary(&mut Unstructured::new(&x)).unwrap(),
&[84148994]
);
#[cfg(feature = "std")]
assert_eq!(
String::arbitrary(&mut Unstructured::new(&x)).unwrap(),
"\x01\x02\x03\x04\x05\x06\x07\x08"
Expand All @@ -1084,6 +1122,7 @@ mod test {
Vec::<u32>::arbitrary_take_rest(Unstructured::new(&x)).unwrap(),
&[0x4030201]
);
#[cfg(feature = "std")]
assert_eq!(
String::arbitrary_take_rest(Unstructured::new(&x)).unwrap(),
"\x01\x02\x03\x04"
Expand Down
4 changes: 2 additions & 2 deletions src/size_hint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ pub fn and_all(hints: &[(usize, Option<usize>)]) -> (usize, Option<usize>) {
/// `lhs` and `rhs` size hints.
#[inline]
pub fn or(lhs: (usize, Option<usize>), rhs: (usize, Option<usize>)) -> (usize, Option<usize>) {
let lower = std::cmp::min(lhs.0, rhs.0);
let lower = core::cmp::min(lhs.0, rhs.0);
let upper = lhs
.1
.and_then(|lhs| rhs.1.map(|rhs| std::cmp::max(lhs, rhs)));
.and_then(|lhs| rhs.1.map(|rhs| core::cmp::max(lhs, rhs)));
(lower, upper)
}

Expand Down
Loading