Stadium

Struct Stadium 

Source
pub struct Stadium<'a> { /* private fields */ }
Expand description

A chunk of allocated memory that stores a bunch of values of different types.

§Examples

let mut builder = stadium::builder();

let h_vec = builder.insert(vec![1, 2, 3, 4]);
let h_string = builder.insert(String::from("Hello"));
let h_str = builder.insert("World");

let mut stadium = builder.build();

stadium[h_vec][0] = 2;
assert_eq!(&stadium[h_vec][..], &[2, 2, 3, 4]);

assert_eq!(stadium[h_str], "World");

Note that using a String or a Vec inside of a Stadium defies a bit of its original purpose (which is storing those different types localy in memory).

Implementations§

Source§

impl<'a> Stadium<'a>

Source

pub fn builder() -> Builder<'a>

Creates a new Builder.

§Examples
use stadium::Stadium;

let builder = Stadium::builder();

Builder struct.Builder.html

Source

pub fn is_associated_with<T: 'a>(&self, handle: Handle<T>) -> bool

Checks if the given Handle can be safely used with this Stadium.

§Examples
let mut builder_1 = stadium::builder();
let handle_1 = builder_1.insert("I'm a string inserted in the first stadium");
let stadium_1 = builder_1.build();

let mut builder_2 = stadium::builder();
let handle_2 = builder_2.insert("I'm a string inserted in the second stadium");
let stadium_2 = builder_2.build();

assert_eq!(stadium_1.is_associated_with(handle_2), false);
assert_eq!(stadium_1.is_associated_with(handle_1), true);
assert_eq!(stadium_2.is_associated_with(handle_2), true);
assert_eq!(stadium_2.is_associated_with(handle_1), false);
Source

pub unsafe fn replace_unchecked<T: 'a>( &mut self, handle: Handle<T>, val: T, ) -> T

Replaces the object referenced by the given Handle.

§Safety

The provided Handle must be associated with this Stadium.

To check if a Handle can be safely used with a given Stadium, use the Stadium::is_associated_with function.

§Examples
let mut builder = stadium::builder();
let handle = builder.insert(4);
let mut stadium = builder.build();

// SAFETY: The handle was created for this stadium.
unsafe {
    assert_eq!(stadium.replace_unchecked(handle, 5), 4);
    assert_eq!(stadium.get_unchecked(handle), &5);
}
Source

pub fn replace<T: 'a>(&mut self, handle: Handle<T>, val: T) -> T

Replaces the object referenced by the given Handle with the given value.

§Panics

This function panics if handle is not associated with this Stadium.

To check if a Handle can be safely used with a given Stadium, use the Stadium::is_associated_with function.

§Examples
let mut builder = stadium::builder();
let handle = builder.insert(5);
let mut stadium = builder.build();

assert_eq!(stadium.replace(handle, 6), 5);
assert_eq!(stadium.get(handle), &6);
Source

pub fn get<T: 'a>(&self, handle: Handle<T>) -> &T

Gets a reference to a value that is part of the Stadium.

§Panics

This function panics if handle is not associated with this Stadium.

To check if a Handle can be safely used with a given Stadium, use the Stadium::is_associated_with function.

§Examples
let mut builder = stadium::builder();

let h_num = builder.insert(2023);
let h_str = builder.insert("Hello, world");

let stadium = builder.build();

assert_eq!(stadium.get(h_str), &"Hello, world");
assert_eq!(stadium.get(h_num), &2023);
Source

pub fn get_mut<T: 'a>(&mut self, handle: Handle<T>) -> &mut T

Gets a reference to a value that is part of the Stadium.

§Panics

This function panics if handle is not associated with this Stadium.

To check if a Handle can be safely used with a given Stadium, use the Stadium::is_associated_with function.

§Examples
let mut builder = stadium::builder();

let h_num = builder.insert(250);
let h_vec = builder.insert(vec![1, 2, 3]);

let mut stadium = builder.build();

*stadium.get_mut(h_num) = 5;
stadium.get_mut(h_vec).push(4);

assert_eq!(stadium.get(h_num), &5);
assert_eq!(&stadium.get(h_vec)[..], &[1, 2, 3, 4])
Source

pub unsafe fn get_unchecked<T: 'a>(&self, handle: Handle<T>) -> &T

Gets a reference to a value that is part of the Stadium.

§Safety

The provided Handle must be associated with this Stadium.

To check if a Handle can be safely used with a given Stadium, use the Stadium::is_associated_with function.

§Examples
let mut builder = stadium::builder();
let handle = builder.insert(5);
let mut stadium = builder.build();

// SAFETY: The handle was provided by the builder of this stadium.
unsafe { assert_eq!(stadium.get_unchecked(handle), &5) };
Source

pub unsafe fn get_unchecked_mut<T: 'a>(&mut self, handle: Handle<T>) -> &mut T

Gets a reference to a value that is part of the Stadium.

§Safety

The provided Handle must be associated with this Stadium.

To check if a Handle can be safely used with a given Stadium, use the Stadium::is_associated_with function.

§Examples
let mut builder = stadium::builder();
let handle = builder.insert(5);
let mut stadium = builder.build();

// SAFETY: The handle was provided by the builder of this stadium.
unsafe {
    *stadium.get_unchecked_mut(handle) = 4;
    assert_eq!(stadium.get_unchecked(handle), &4);
}
Source

pub unsafe fn get_ptr_raw(&self, handle: RawHandle) -> *const u8

Gets a pointer to the element referenced by the given RawHandle.

§Safety

This function is unsafe unless:

  • The given Handle is associated with this Stadium.
  • The returned pointer is used as if it was a *const T where T is the type of the original Handle (it was Handle<T>).

To check if a Handle can be safely used with a given Stadium, use the Stadium::is_associated_with function.

Source

pub unsafe fn get_ptr_mut_raw(&mut self, handle: RawHandle) -> *mut u8

Gets a pointer to the element referenced by the given RawHandle.

§Safety

This function is unsafe unless:

  • The given Handle is associated with this Stadium.
  • The returned pointer is used as if it was a *mut T where T is the type of the original Handle (it was Handle<T>).

To check if a Handle can be safely used with a given Stadium, use the Stadium::is_associated_with function.

Source

pub unsafe fn get_ptr<T: 'a>(&self, handle: Handle<T>) -> *const T

Gets a pointer to the element referenced by the given Handle.

§Safety

The given Handle must be associated with this Stadium.

To check if a Handle can be safely used with a given Stadium, use the Stadium::is_associated_with function.

Source

pub unsafe fn get_ptr_mut<T: 'a>(&mut self, handle: Handle<T>) -> *mut T

Gets a pointer to the element referenced by the given Handle.

§Safety

The given Handle must be associated with this Stadium.

To check if a Handle can be safely used with a given Stadium, use the Stadium::is_associated_with function.

Source

pub unsafe fn swap_unchecked<T: 'a>(&mut self, a: Handle<T>, b: Handle<T>)

Swaps the values referenced by a and b within this Stadium.

§Safety
  • This given handles a and b must both be associated with this Stadium.
  • a must be different from b
§Examples
let mut builder = stadium::builder();
let a = builder.insert("Foo");
let b = builder.insert("Bar");
let mut s = builder.build();

assert_eq!(s[a], "Foo");
assert_eq!(s[b], "Bar");

// SAFETY: Those two handles are associated with `s`.
unsafe { s.swap_unchecked(a, b); }

assert_eq!(s[a], "Bar");
assert_eq!(s[b], "Foo");
Source

pub fn swap<T: 'a>(&mut self, a: Handle<T>, b: Handle<T>)

Swaps the values referenced by a and b within this Stadium.

§Panics

This function panics if one of a or b is not associated with tihs Stadium.

To check if a Handle can be safely used with a given Stadium, use the Stadium::is_associated_with function.

§Examples
let mut builder = stadium::builder();
let a = builder.insert("Foo");
let b = builder.insert("Bar");
let mut s = builder.build();

assert_eq!(s[a], "Foo");
assert_eq!(s[b], "Bar");

s.swap(a, b);

assert_eq!(s[a], "Bar");
assert_eq!(s[b], "Foo");

Trait Implementations§

Source§

impl Drop for Stadium<'_>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<'a> From<Builder<'a>> for Stadium<'a>

Source§

fn from(builder: Builder<'a>) -> Self

Converts to this type from the input type.
Source§

impl<'a, T: 'a> Index<Handle<T>> for Stadium<'a>

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, handle: Handle<T>) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<'a, T: 'a> IndexMut<Handle<T>> for Stadium<'a>

Source§

fn index_mut(&mut self, handle: Handle<T>) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for Stadium<'a>

§

impl<'a> RefUnwindSafe for Stadium<'a>

§

impl<'a> !Send for Stadium<'a>

§

impl<'a> !Sync for Stadium<'a>

§

impl<'a> Unpin for Stadium<'a>

§

impl<'a> UnwindSafe for Stadium<'a>

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