diff --git a/CHANGELOG.md b/CHANGELOG.md index e4b5b72..ce0a4ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - `StableVec::insert_into_hole()` - `StableVec::grow()` - `StableVec::clear()` +- `StableVec::from_vec()` +- `StableVec::extend_from_slice()` +- `Debug` implementations for `Iter`, `IterMut` and `Keys` +- `Write` implementation for `StableVec` ### Changed - The `Drop` impl now uses the `mem::needs_drop()` optimization hint to avoid diff --git a/Cargo.toml b/Cargo.toml index 8c6a25c..3ea7932 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,4 +22,4 @@ maintenance = { status = "actively-developed" } bit-vec = "0.5.0" [dev-dependencies] -quickcheck = "0.6.2" +quickcheck = "0.7" diff --git a/src/lib.rs b/src/lib.rs index fc44266..76f5cdc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,6 +22,8 @@ //! use stable_vec::StableVec; //! ``` +#![deny(missing_debug_implementations)] + extern crate bit_vec; #[cfg(test)] #[macro_use] @@ -32,6 +34,7 @@ use bit_vec::BitVec; use std::fmt; use std::iter::FromIterator; use std::mem; +use std::io; use std::ops::{Index, IndexMut}; use std::ptr; @@ -192,6 +195,33 @@ impl StableVec { } } + /// Creates a `StableVec` from the given `Vec`. The elements are not + /// copied and the indices of the vector are preserved. + /// + /// Note that this function will still allocate memory to store meta data. + /// + /// # Example + /// + /// ``` + /// # use stable_vec::StableVec; + /// let mut sv = StableVec::from_vec(vec!['★', '♥']); + /// + /// assert_eq!(sv.get(0), Some(&'★')); + /// assert_eq!(sv.get(1), Some(&'♥')); + /// assert_eq!(sv.num_elements(), 2); + /// assert!(sv.is_compact()); + /// + /// sv.remove(0); + /// assert_eq!(sv.get(1), Some(&'♥')); + /// ``` + pub fn from_vec(vec: Vec) -> Self { + Self { + used_count: vec.len(), + deleted: BitVec::from_elem(vec.len(), false), + data: vec, + } + } + /// Reserves capacity for at least `additional` more elements to be /// inserted. pub fn reserve(&mut self, additional: usize) { @@ -630,8 +660,9 @@ impl StableVec { /// Removes all elements from this collection. /// - /// After calling this, `num_elements()` will return 0. However, no memory - /// is deallocated, so the capacity stays as it was before. + /// After calling this, `num_elements()` will return 0. All indices are + /// invalidated. However, no memory is deallocated, so the capacity stays + /// as it was before. /// /// # Example /// @@ -876,6 +907,18 @@ impl StableVec { } } } + + /// Appends all elements in `new_elements` to this `StableVec`. This is + /// equivalent to calling [`push()`][StableVec::push] for each element. + pub fn extend_from_slice(&mut self, new_elements: &[T]) + where + T: Clone, + { + // This could be improved for `Copy` elements via specialization. + for elem in new_elements { + self.push(elem.clone()); + } + } } impl Drop for StableVec { @@ -984,6 +1027,22 @@ impl Extend for StableVec { } } +/// Write into `StableVec` by appending `u8` elements. This is equivalent +/// to calling `push` for each byte. +impl io::Write for StableVec { + fn write(&mut self, buf: &[u8]) -> io::Result { + self.extend_from_slice(buf); + Ok(buf.len()) + } + + fn write_all(&mut self, buf: &[u8]) -> io::Result<()> { + self.extend_from_slice(buf); + Ok(()) + } + + fn flush(&mut self) -> io::Result<()> { Ok(()) } +} + impl<'a, T> IntoIterator for &'a StableVec { type Item = &'a T; type IntoIter = Iter<'a, T>; @@ -1005,6 +1064,7 @@ impl<'a, T> IntoIterator for &'a mut StableVec { /// Use the method [`StableVec::iter()`](struct.StableVec.html#method.iter) or /// the `IntoIterator` implementation of `&StableVec` to obtain an iterator /// of this kind. +#[derive(Debug)] pub struct Iter<'a, T: 'a> { sv: &'a StableVec, pos: usize, @@ -1023,6 +1083,7 @@ impl<'a, T: 'a> Iterator for Iter<'a, T> { /// Use the method [`StableVec::iter_mut()`](struct.StableVec.html#method.iter_mut) /// or the `IntoIterator` implementation of `&mut StableVec` to obtain an /// iterator of this kind. +#[derive(Debug)] pub struct IterMut<'a, T: 'a> { deleted: &'a mut BitVec, used_count: &'a mut usize, @@ -1072,6 +1133,7 @@ impl<'a, T> Iterator for IterMut<'a, T> { /// /// Use the method [`StableVec::keys()`](struct.StableVec.html#method.keys) to /// obtain an iterator of this kind. +#[derive(Debug)] pub struct Keys<'a> { deleted: &'a BitVec, pos: usize,