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>
impl<'a> Stadium<'a>
Sourcepub fn is_associated_with<T: 'a>(&self, handle: Handle<T>) -> bool
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);Sourcepub unsafe fn replace_unchecked<T: 'a>(
&mut self,
handle: Handle<T>,
val: T,
) -> T
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);
}Sourcepub fn replace<T: 'a>(&mut self, handle: Handle<T>, val: T) -> T
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);Sourcepub fn get<T: 'a>(&self, handle: Handle<T>) -> &T
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);Sourcepub fn get_mut<T: 'a>(&mut self, handle: Handle<T>) -> &mut T
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])Sourcepub unsafe fn get_unchecked<T: 'a>(&self, handle: Handle<T>) -> &T
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) };Sourcepub unsafe fn get_unchecked_mut<T: 'a>(&mut self, handle: Handle<T>) -> &mut T
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);
}Sourcepub unsafe fn get_ptr_raw(&self, handle: RawHandle) -> *const u8
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
Handleis associated with thisStadium. - The returned pointer is used as if it was a
*const TwhereTis the type of the originalHandle(it wasHandle<T>).
To check if a Handle can be safely used with a given Stadium, use the
Stadium::is_associated_with function.
Sourcepub unsafe fn get_ptr_mut_raw(&mut self, handle: RawHandle) -> *mut u8
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
Handleis associated with thisStadium. - The returned pointer is used as if it was a
*mut TwhereTis the type of the originalHandle(it wasHandle<T>).
To check if a Handle can be safely used with a given Stadium, use the
Stadium::is_associated_with function.
Sourcepub unsafe fn get_ptr_mut<T: 'a>(&mut self, handle: Handle<T>) -> *mut T
pub unsafe fn get_ptr_mut<T: 'a>(&mut self, handle: Handle<T>) -> *mut T
Sourcepub unsafe fn swap_unchecked<T: 'a>(&mut self, a: Handle<T>, b: Handle<T>)
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
aandbmust both be associated with thisStadium. amust be different fromb
§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");Sourcepub fn swap<T: 'a>(&mut self, a: Handle<T>, b: Handle<T>)
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");