Skip to main content

Crate spaghetto

Crate spaghetto 

Source
Expand description

§spaghetto

spaghetto is a library for creating double-ended data structures that can be rolled out or eaten on from either side. This includes the base structure DeVec, a double-ended Vec that can be used as a deque, and DeString, a double-ended alternative to String.


§Examples

  • A DeVec can be used as a double-ended queue, but with the added benefit of being able to get a single contiguous slice of the entire structure.
use spaghetto::DeVec;
let mut devec = DeVec::with_capacity(5);
devec.push_back(1);
devec.push_front(2);
devec.push_back(3);
assert_eq!(devec.pop_front(), Some(2));
devec.pop_front();
// Now let's overload one side.
// This forces a DeVec to reallocate in order to keep the elements contiguous.
// With a VecDeque this would no longer be a contiguous allocation
// because we moved the front of the queue into the middle of the buffer
// and pushed it out past the end of the buffer.
// It would need to wrap around.
for i in 0..3 {
   devec.push_back(i);
}

// we can get a single contiguous slice of the entire DeVec without having to shift elements.
let no_mutation = &devec;
let slice = no_mutation.as_slice();
  • A DeString can be used as a double-ended string, and because of this, we can efficiently remove extra whitespace from either side, mutating in place and maintining a single contiguous string slice without the cost of shifting like with a String.
use spaghetto::DeString;

let mut destring = DeString::from("  hello world  ");
destring.mut_trim_front();

// It is contiguous and also mutated its starting position!
assert_eq!(destring.as_str(), "hello world  ");

Re-exports§

pub use destring::DeString;
pub use devec::DeVec;
pub use settings::*;

Modules§

destring
A module to hold DeString, a string type that uses a DeVec internally.
devec
A DeVec is a double-ended vector that is meant to handle easy insertions and deletions from either side. Its API is similar to VecDeque, but with some conveniences from Vec, both because of its unique properties and because we can see the nightly implementations before they are stabilized. This data structure has benefits over a VecDeque in that it can maintain a contiguous allocation for its entire life. This means that it can easily be used with functions that require a contiguous slice of memory.
settings

Macros§

devec
A version of the vec! macro but for DeVec. This macro helps you create a DeVec from a list of elements. It will use the default drop order of FrontToBack and rebalance strategy of putting the center of the elements into the Middle of the allocation after a resize.

Type Aliases§

Spaghetto
The default config for a DeVec<T>, named after a single piece of spaghetti.