Struct BumpyVector

Source
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.

Source

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.

Source

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());
Source

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());
Source

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());
Source

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);
Source

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);
Source

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);
Source

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());
Source

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).

Source

pub fn len(&self) -> usize

Returns the number of entries.

Source

pub fn max_size(&self) -> usize

Trait Implementations§

Source§

impl<T: Clone> Clone for BumpyVector<T>

Source§

fn clone(&self) -> BumpyVector<T>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for BumpyVector<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Default> Default for BumpyVector<T>

Source§

fn default() -> BumpyVector<T>

Returns the “default value” for a type. Read more
Source§

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>,

Deserialize this value from the given Serde deserializer. Read more
Source§

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.

Source§

type Item = &'a BumpyEntry<T>

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<&'a BumpyEntry<T>>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> IntoIter<&'a BumpyEntry<T>>

Creates an iterator from a value. Read more
Source§

impl<T> Serialize for BumpyVector<T>
where T: Serialize,

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl<T> Freeze for BumpyVector<T>

§

impl<T> RefUnwindSafe for BumpyVector<T>
where T: RefUnwindSafe,

§

impl<T> Send for BumpyVector<T>
where T: Send,

§

impl<T> Sync for BumpyVector<T>
where T: Sync,

§

impl<T> Unpin for BumpyVector<T>
where T: Unpin,

§

impl<T> UnwindSafe for BumpyVector<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,