pub struct BumpyVector<T> { /* private fields */ }
Expand description
Represents an instance of a Bumpy Vector
Implementations§
Source§impl<'a, T> BumpyVector<T>
Implement the object.
impl<'a, T> BumpyVector<T>
Implement the object.
Sourcepub fn new(max_size: usize) -> Self
pub fn new(max_size: usize) -> Self
Create a new instance of BumpyVector.
The range of the vector goes from 0
to max_size - 1
. If any
elements beyond the end are accessed, an error will be returned.
Sourcepub fn insert(&mut self, entry: BumpyEntry<T>) -> Result<(), &'static str>
pub fn insert(&mut self, entry: BumpyEntry<T>) -> Result<(), &'static str>
Insert a new entry.
§Return
Returns Ok(())
if successfully inserted. If it would overlap another
entry or exceed max_size
, return Err(&str)
with a descriptive error
string.
Size must be at least 1.
§Example
use bumpy_vector::{BumpyEntry, BumpyVector};
// Create a 10-byte `BumpyVector`
let mut v: BumpyVector<&str> = BumpyVector::new(10);
// Insert a 2-byte value starting at index 5 (using BumpyEntry directly)
assert!(v.insert(BumpyEntry { entry: "hello", index: 5, size: 2 }).is_ok());
// Insert another 2-byte value starting at index 7 (using into())
assert!(v.insert(("hello", 7, 2).into()).is_ok());
// Fail to insert a value that would overlap the first
assert!(v.insert(("hello", 4, 2).into()).is_err());
// Fail to insert a value that would overlap the second
assert!(v.insert(("hello", 6, 1).into()).is_err());
// Fail to insert a value that would go out of bounds
assert!(v.insert(("hello", 100, 1).into()).is_err());
Sourcepub fn remove(&mut self, index: usize) -> Option<BumpyEntry<T>>
pub fn remove(&mut self, index: usize) -> Option<BumpyEntry<T>>
Remove and return the entry at index
.
Note that the entry doesn’t necessarily need to start at index
,
just overlap it.
§Example
use bumpy_vector::BumpyVector;
// Create a 10-byte `BumpyVector`
let mut v: BumpyVector<&str> = BumpyVector::new(10);
// Insert some data
v.insert(("hello", 0, 4).into()).unwrap();
v.insert(("hello", 4, 4).into()).unwrap();
assert!(v.remove(0).is_some());
assert!(v.remove(0).is_none());
assert!(v.remove(6).is_some());
assert!(v.remove(6).is_none());
Sourcepub fn remove_range(
&mut self,
index: usize,
length: usize,
) -> Vec<BumpyEntry<T>>
pub fn remove_range( &mut self, index: usize, length: usize, ) -> Vec<BumpyEntry<T>>
Remove and return a range of entries.
§Example
use bumpy_vector::BumpyVector;
// Create a 10-byte `BumpyVector`
let mut v: BumpyVector<&str> = BumpyVector::new(10);
// Insert some data
v.insert(("hello", 0, 4).into()).unwrap();
v.insert(("hello", 4, 4).into()).unwrap();
assert_eq!(2, v.remove_range(0, 10).len());
assert_eq!(0, v.remove_range(0, 10).len());
Sourcepub fn get(&self, index: usize) -> Option<&BumpyEntry<T>>
pub fn get(&self, index: usize) -> Option<&BumpyEntry<T>>
Return a reference to an entry at the given index.
Note that the entry doesn’t necessarily need to start at the given index, it can simply be contained there.
§Example
use bumpy_vector::BumpyVector;
// Create a 10-byte `BumpyVector`
let mut v: BumpyVector<&str> = BumpyVector::new(10);
// Insert some data
v.insert(("hello", 0, 4).into()).unwrap();
assert!(v.get(0).is_some());
assert!(v.get(1).is_some());
assert!(v.get(2).is_some());
assert!(v.get(3).is_some());
assert!(v.get(4).is_none());
assert!(v.get(5).is_none());
assert_eq!("hello", v.get(0).unwrap().entry);
assert_eq!("hello", v.get(1).unwrap().entry);
assert_eq!("hello", v.get(2).unwrap().entry);
assert_eq!("hello", v.get(3).unwrap().entry);
Sourcepub fn get_mut(&mut self, index: usize) -> Option<&mut BumpyEntry<T>>
pub fn get_mut(&mut self, index: usize) -> Option<&mut BumpyEntry<T>>
Return a mutable reference to an entry at the given index.
§Example
use bumpy_vector::BumpyVector;
// Create a small BumpyVector
let mut h: BumpyVector<String> = BumpyVector::new(10);
// Insert a string to the start
h.insert((String::from("hello"), 0, 2).into()).unwrap();
assert_eq!("hello", h.get(0).unwrap().entry);
assert_eq!("hello", h.get(1).unwrap().entry);
// Get a mutable reference to the string
let s = h.get_mut(1).unwrap();
// Modify it somehow
s.entry.make_ascii_uppercase();
// Verify it's changed
assert_eq!("HELLO", h.get(0).unwrap().entry);
assert_eq!("HELLO", h.get(1).unwrap().entry);
Sourcepub fn get_exact(&self, index: usize) -> Option<&BumpyEntry<T>>
pub fn get_exact(&self, index: usize) -> Option<&BumpyEntry<T>>
Return a reference to an entry that starts at the given index.
§Example
use bumpy_vector::BumpyVector;
// Create a 10-byte `BumpyVector`
let mut v: BumpyVector<&str> = BumpyVector::new(10);
// Insert some data
v.insert(("hello", 0, 4).into()).unwrap();
assert!(v.get_exact(0).is_some());
assert!(v.get_exact(1).is_none());
assert!(v.get_exact(2).is_none());
assert!(v.get_exact(3).is_none());
assert!(v.get_exact(4).is_none());
assert!(v.get_exact(5).is_none());
assert_eq!("hello", v.get_exact(0).unwrap().entry);
Sourcepub fn get_exact_mut(&mut self, index: usize) -> Option<&mut BumpyEntry<T>>
pub fn get_exact_mut(&mut self, index: usize) -> Option<&mut BumpyEntry<T>>
Return a mutable reference to an entry at exactly the given index.
§Example
use bumpy_vector::BumpyVector;
// Create a small BumpyVector
let mut h: BumpyVector<String> = BumpyVector::new(10);
// Insert a string to the start
h.insert((String::from("hello"), 0, 2).into()).unwrap();
assert_eq!("hello", h.get_exact(0).unwrap().entry);
assert!(h.get_exact(1).is_none());
// Get a mutable reference to the string
let s = h.get_exact_mut(0).unwrap();
// Modify it somehow
s.entry.make_ascii_uppercase();
// Verify it's changed
assert_eq!("HELLO", h.get_exact(0).unwrap().entry);
assert!(h.get_exact(1).is_none());
Sourcepub fn get_range(&self, start: usize, length: usize) -> Vec<&BumpyEntry<T>>
pub fn get_range(&self, start: usize, length: usize) -> Vec<&BumpyEntry<T>>
Return a vector of entries within the given range.
Note that the first entry doesn’t need to start at the given index it can simply be contained there.
§Parameters
start
- The starting index.length
- The length to retrieve.
§Example
use bumpy_vector::BumpyVector;
// Create a 10-byte `BumpyVector`
let mut v: BumpyVector<&str> = BumpyVector::new(10);
// Insert some data with a gap in the middle
v.insert(("hello", 0, 2).into()).unwrap();
v.insert(("hello", 4, 2).into()).unwrap();
assert_eq!(1, v.get_range(0, 1).len());
assert_eq!(1, v.get_range(0, 2).len());
assert_eq!(1, v.get_range(0, 3).len());
assert_eq!(1, v.get_range(0, 4).len());
assert_eq!(2, v.get_range(0, 5).len());
§Panics
Panics if an entry’s size is 0. That shouldn’t be possible short of tinkering with internal state (most likely modifying serialized data).
pub fn max_size(&self) -> usize
Trait Implementations§
Source§impl<T: Clone> Clone for BumpyVector<T>
impl<T: Clone> Clone for BumpyVector<T>
Source§fn clone(&self) -> BumpyVector<T>
fn clone(&self) -> BumpyVector<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl<T: Debug> Debug for BumpyVector<T>
impl<T: Debug> Debug for BumpyVector<T>
Source§impl<T: Default> Default for BumpyVector<T>
impl<T: Default> Default for BumpyVector<T>
Source§fn default() -> BumpyVector<T>
fn default() -> BumpyVector<T>
Source§impl<'de, T> Deserialize<'de> for BumpyVector<T>where
T: Deserialize<'de>,
impl<'de, T> Deserialize<'de> for BumpyVector<T>where
T: Deserialize<'de>,
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl<'a, T> IntoIterator for &'a BumpyVector<T>
Convert into an iterator.
impl<'a, T> IntoIterator for &'a BumpyVector<T>
Convert into an iterator.
Naively iterate across all entries, move them into a Vec<_>
, and convert
that vector into an iterator.