From 595db1ffcc75da75b201e70aa8bf0fa62b9d519b Mon Sep 17 00:00:00 2001 From: bluss Date: Sat, 6 Feb 2016 20:14:43 +0100 Subject: [PATCH] Add feature "std" to arrayvec: Allow opting out of libstd --- .travis.yml | 1 + Cargo.toml | 12 +++++++++--- src/array_string.rs | 8 ++++---- src/lib.rs | 34 +++++++++++++++++++++++++++++----- 4 files changed, 43 insertions(+), 12 deletions(-) diff --git a/.travis.yml b/.travis.yml index 01ba9f2..00e87dc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -19,6 +19,7 @@ branches: script: - | ([ ! -z "$NODROP_FEATURES" ] || cargo build --verbose --features "$FEATURES") && + ([ "$NODEFAULT" != 1 ] || cargo build --verbose --no-default-features) && ([ ! -z "$NODROP_FEATURES" ] || cargo test --verbose --features "$FEATURES") && ([ ! -z "$NODROP_FEATURES" ] || cargo test --release --verbose --features "$FEATURES") && ([ ! -z "$NODROP_FEATURES" ] || cargo bench --verbose --features "$FEATURES" -- --test) && diff --git a/Cargo.toml b/Cargo.toml index b8866e0..3dbdb8e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,11 +8,17 @@ description = "A vector with a fixed capacity, it can be stored on the stack too documentation = "http://bluss.github.io/arrayvec" repository = "https://github.com/bluss/arrayvec" -keywords = ["stack", "vector", "array", "string", "data-structure"] +keywords = ["stack", "vector", "array", "data-structure", "no_std"] [dependencies.odds] -version = "0.2" +version = "0.2.12" +default-features = false [dependencies.nodrop] -version = "0.1.4" +version = "0.1.6" path = "nodrop" +default-features = false + +[features] +default = ["std"] +std = ["odds/std", "nodrop/std"] diff --git a/src/array_string.rs b/src/array_string.rs index 06960e9..1c7f814 100644 --- a/src/array_string.rs +++ b/src/array_string.rs @@ -2,6 +2,7 @@ use std::borrow::Borrow; use std::fmt; use std::hash::{Hash, Hasher}; use std::mem; +use std::ptr; use std::ops::{Deref, DerefMut}; use std::str; use std::slice; @@ -116,14 +117,13 @@ impl> ArrayString { /// assert_eq!(overflow2.unwrap_err().element(), "ef"); /// ``` pub fn push_str<'a>(&mut self, s: &'a str) -> Result<(), CapacityError<&'a str>> { - use std::io::Write; - if s.len() > self.capacity() - self.len() { return Err(CapacityError::new(s)); } unsafe { - let sl = slice::from_raw_parts_mut(self.xs.as_mut_ptr(), A::capacity()); - (&mut sl[self.len()..]).write(s.as_bytes()).unwrap(); + let dst = self.xs.as_mut_ptr().offset(self.len() as isize); + let src = s.as_ptr(); + ptr::copy_nonoverlapping(src, dst, s.len()); let newl = self.len() + s.len(); self.set_len(newl); } diff --git a/src/lib.rs b/src/lib.rs index ad576c5..77aac25 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,8 +1,20 @@ +//! **arrayvec** provides the types `ArrayVec` and `ArrayString`: +//! array-backed vector and string types, which store their contents inline. +//! +//! The **arrayvec** crate has the following cargo feature flags: +//! +//! - `std` +//! - Optional, enabled by default +//! - Requires Rust 1.6 *to disable* +//! - Use libstd +#![cfg_attr(not(feature="std"), no_std)] extern crate odds; extern crate nodrop; +#[cfg(not(feature="std"))] +extern crate core as std; + use std::cmp; -use std::io; use std::iter; use std::mem; use std::ptr; @@ -12,15 +24,20 @@ use std::ops::{ }; use std::slice; -use nodrop::NoDrop; - // extra traits -use std::any::Any; use std::borrow::{Borrow, BorrowMut}; -use std::error::Error; use std::hash::{Hash, Hasher}; use std::fmt; +#[cfg(feature="std")] +use std::io; +#[cfg(feature="std")] +use std::error::Error; +#[cfg(feature="std")] +use std::any::Any; // core but unused + +use nodrop::NoDrop; + mod array; mod array_string; @@ -686,7 +703,10 @@ impl Ord for ArrayVec where A::Item: Ord { } } +#[cfg(feature="std")] /// `Write` appends written data to the end of the vector. +/// +/// Requires `features="std"`. impl> io::Write for ArrayVec { fn write(&mut self, data: &[u8]) -> io::Result { unsafe { @@ -729,12 +749,16 @@ impl CapacityError { const CAPERROR: &'static str = "insufficient capacity"; +#[cfg(feature="std")] +/// Requires `features="std"`. impl Error for CapacityError { fn description(&self) -> &str { CAPERROR } } +#[cfg(feature="std")] +/// Requires `features="std"`. impl fmt::Display for CapacityError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", CAPERROR)