Skip to content

Commit

Permalink
Merge pull request #17 from LukasKalbertodt/several-api-additions
Browse files Browse the repository at this point in the history
Several API additions
  • Loading branch information
LukasKalbertodt authored Sep 26, 2018
2 parents 7d14ba4 + de7a40b commit c96fd39
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>`

### Changed
- The `Drop` impl now uses the `mem::needs_drop()` optimization hint to avoid
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ maintenance = { status = "actively-developed" }
bit-vec = "0.5.0"

[dev-dependencies]
quickcheck = "0.6.2"
quickcheck = "0.7"
66 changes: 64 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
//! use stable_vec::StableVec;
//! ```
#![deny(missing_debug_implementations)]

extern crate bit_vec;
#[cfg(test)]
#[macro_use]
Expand All @@ -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;

Expand Down Expand Up @@ -192,6 +195,33 @@ impl<T> StableVec<T> {
}
}

/// Creates a `StableVec<T>` from the given `Vec<T>`. 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<T>) -> 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) {
Expand Down Expand Up @@ -630,8 +660,9 @@ impl<T> StableVec<T> {

/// 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
///
Expand Down Expand Up @@ -876,6 +907,18 @@ impl<T> StableVec<T> {
}
}
}

/// Appends all elements in `new_elements` to this `StableVec<T>`. 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<T> Drop for StableVec<T> {
Expand Down Expand Up @@ -984,6 +1027,22 @@ impl<T> Extend<T> for StableVec<T> {
}
}

/// Write into `StableVec<u8>` by appending `u8` elements. This is equivalent
/// to calling `push` for each byte.
impl io::Write for StableVec<u8> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
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<T> {
type Item = &'a T;
type IntoIter = Iter<'a, T>;
Expand All @@ -1005,6 +1064,7 @@ impl<'a, T> IntoIterator for &'a mut StableVec<T> {
/// 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<T>,
pos: usize,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit c96fd39

Please sign in to comment.