Struct ByteBox

Source
pub struct ByteBox { /* private fields */ }
Expand description

A hash table implementation that stores key-value pairs as byte vectors. Uses separate chaining to handle hash collisions.

§Examples

use bytesbox::ByteBox;

let mut bytebox = ByteBox::new();
bytebox.insert(b"key1", b"value1");
bytebox.insert(b"key2", b"value2");

assert_eq!(bytebox.get(b"key1"), Some(&b"value1"[..]));
assert_eq!(bytebox.len(), 2);

Implementations§

Source§

impl ByteBox

Source

pub fn new() -> Self

Creates a new ByteBox with a default initial capacity of 16 cells.

§Examples
use bytesbox::ByteBox;

let bytebox = ByteBox::new();
assert_eq!(bytebox.len(), 0);
Source

pub fn prealloc(size: usize) -> Self

Creates a new ByteBox with a specified initial capacity.

§Arguments
  • size - The initial number of cells to allocate.
§Examples
use bytesbox::ByteBox;

let bytebox = ByteBox::prealloc(32);
assert_eq!(bytebox.allocation(), 32);
Source

pub fn len(&self) -> usize

Returns the number of key-value pairs stored in the ByteBox.

§Examples
use bytesbox::ByteBox;

let mut bytebox = ByteBox::new();
assert_eq!(bytebox.len(), 0);
bytebox.insert(b"key", b"value");
assert_eq!(bytebox.len(), 1);
Source

pub fn allocation(&self) -> usize

Returns the current allocation size (number of cells) of the ByteBox.

§Examples
use bytesbox::ByteBox;

let bytebox = ByteBox::new();
assert_eq!(bytebox.allocation(), 16);
Source

pub fn insert(&mut self, key: &[u8], value: &[u8]) -> bool

Inserts a key-value pair into the ByteBox.

If the key already exists, its value is updated. If the load factor exceeds the threshold after insertion, the table is resized.

§Arguments
  • key - A byte slice representing the key.
  • value - A byte slice representing the value.
§Returns
  • true if a new key-value pair was inserted.
  • false if an existing key was updated.
§Examples
use bytesbox::ByteBox;

let mut bytebox = ByteBox::new();
assert!(bytebox.insert(b"key1", b"value1"));
assert!(!bytebox.insert(b"key1", b"value2"));
assert_eq!(bytebox.get(b"key1"), Some(&b"value2"[..]));
Source

pub fn insert_primitive<T: BytesPrimitives>(&mut self, key: &[u8], value: T)

Inserts a key and a primitive value into the ByteBox.

The primitive value is converted to its byte representation using the BytesPrimitives trait.

§Type Parameters
  • T - A type that implements the BytesPrimitives trait.
§Arguments
  • key - A byte slice representing the key.
  • value - A primitive value that can be converted into bytes.
§Examples
use bytesbox::ByteBox;
use bytesbox::primitives::BytesPrimitives;

let mut bytebox = ByteBox::new();
bytebox.insert_primitive(b"number", 42u32);
assert_eq!(bytebox.get(b"number"), Some(&b"42"[..]));
Source

pub fn get(&self, key: &[u8]) -> Option<&[u8]>

Retrieves the value associated with the given key.

§Arguments
  • key - A byte slice representing the key to look up.
§Returns
  • Some(&[u8]) containing the value if the key exists.
  • None if the key does not exist in the ByteBox.
§Examples
use bytesbox::ByteBox;

let mut bytebox = ByteBox::new();
bytebox.insert(b"key", b"value");
assert_eq!(bytebox.get(b"key"), Some(&b"value"[..]));
assert_eq!(bytebox.get(b"nonexistent"), None);
Source

pub fn remove(&mut self, key: &[u8]) -> Option<Vec<u8>>

Removes the key-value pair associated with the given key from the ByteBox.

§Arguments
  • key - A byte slice representing the key to remove.
§Returns
  • Some(Vec<u8>) containing the removed value if the key existed.
  • None if the key was not found.
§Examples
use bytesbox::ByteBox;

let mut bytebox = ByteBox::new();
bytebox.insert(b"key", b"value");
assert_eq!(bytebox.remove(b"key"), Some(b"value".to_vec()));
assert_eq!(bytebox.remove(b"key"), None);
Source

pub fn clear(&mut self)

Removes all key-value pairs from the ByteBox, resetting it to an empty state.

This method retains the current capacity of the hash table, allowing it to be reused without the overhead of reallocating memory. All existing entries are removed, and the length (len) is set to zero.

§Examples
use bytesbox::ByteBox;

let mut bytebox = ByteBox::new();
bytebox.insert(b"key1", b"value1");
bytebox.insert(b"key2", b"value2");
assert_eq!(bytebox.len(), 2);

bytebox.clear();
assert_eq!(bytebox.len(), 0);
assert_eq!(bytebox.get(b"key1"), None);
assert_eq!(bytebox.get(b"key2"), None);
Source

pub fn iter(&self) -> ByteBoxIterator<'_>

Provides an iterator over the ByteBox that allows for iteration using for loops.

This enables the use of ByteBox in contexts where an iterator is expected.

§Examples
use bytesbox::ByteBox;

let mut bytebox = ByteBox::new();
bytebox.insert(b"key1", b"value1");
bytebox.insert(b"key2", b"value2");

for (key, value) in bytebox.iter() {
    println!("{:?}: {:?}", key, value);
}
Source

pub fn view_table(&self)

Trait Implementations§

Source§

impl Clone for ByteBox

Source§

fn clone(&self) -> ByteBox

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 Debug for ByteBox

Source§

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

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

impl Display for ByteBox

Source§

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

Formats the ByteBox for display purposes.

This implementation displays the contents in a readable key-value format.

§Examples
use bytesbox::ByteBox;

let mut bytebox = ByteBox::new();
bytebox.insert(b"key", b"value");
println!("{}", bytebox);

Auto Trait Implementations§

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.