bytesbox

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

Provides a detailed, colored visualization of the hash table.

This function prints the structure of the ByteBox, including each cell and its entries.

§Examples
use bytesbox::ByteBox;

let mut bytebox = ByteBox::new();
bytebox.insert(b"key", b"value");
bytebox.view_table();

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, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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§

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