Add feature "std" to arrayvec: Allow opting out of libstd

This commit is contained in:
bluss
2016-02-06 20:14:43 +01:00
parent 34c45dca50
commit 595db1ffcc
4 changed files with 43 additions and 12 deletions
+4 -4
View File
@@ -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<A: Array<Item=u8>> ArrayString<A> {
/// 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);
}
+29 -5
View File
@@ -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<A: Array> Ord for ArrayVec<A> where A::Item: Ord {
}
}
#[cfg(feature="std")]
/// `Write` appends written data to the end of the vector.
///
/// Requires `features="std"`.
impl<A: Array<Item=u8>> io::Write for ArrayVec<A> {
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
unsafe {
@@ -729,12 +749,16 @@ impl<T> CapacityError<T> {
const CAPERROR: &'static str = "insufficient capacity";
#[cfg(feature="std")]
/// Requires `features="std"`.
impl<T: Any> Error for CapacityError<T> {
fn description(&self) -> &str {
CAPERROR
}
}
#[cfg(feature="std")]
/// Requires `features="std"`.
impl<T> fmt::Display for CapacityError<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", CAPERROR)