pub struct DataBuf<T: ?Sized> { /* private fields */ }Expand description
A wrapper acting as an owned buffer for Raylib-allocated memory.
Automatically releases the memory with ffi::MemFree() when dropped.
§Example
use raylib::prelude::*;
let buf: DataBuf<[u8]> = compress_data(b"11111").unwrap();
// Use this how you used to use the return of `compress_data()`.
// It will live until `buf` goes out of scope or gets dropped.
let data: &[u8] = buf.as_ref();
let expected: &[u8] = &[1, 5, 0, 250, 255, 49, 49, 49, 49, 49];
assert_eq!(data, expected);§Safety
bufmust not be dangling.bufmust be safe to dereference.bufmust be a unique, owned pointer (not to static or local memory, and the memory must not be accessible through any pointers/references not derived from the returnedDataBuf).bufmust point to valid, initialized data.bufmust be convertible to a reference.bufmust have been created withRL_MALLOC/ffi::MemAllocorRL_REALLOC/ffi::MemRealloc.
This structure is only intended for use with pointers given by Raylib with the expectation that you
would manually deallocate them with ffi::MemFree. DO NOT use this structure to hold arbitrary
or un-owned pointers.
If the pointer is expected to be conditionally deallocated by Raylib,
(i.e. conditionally passing the buffer to a Raylib function that will certainly deallocatate it)
use DataBuf::into_inner to prevent the Drop impl from causing a double-free.
Implementations§
Source§impl<T> DataBuf<MaybeUninit<T>>
impl<T> DataBuf<MaybeUninit<T>>
Source§impl<T> DataBuf<[MaybeUninit<T>]>
impl<T> DataBuf<[MaybeUninit<T>]>
Sourcepub const unsafe fn assume_init(self) -> DataBuf<[T]>
pub const unsafe fn assume_init(self) -> DataBuf<[T]>
Mark that the data pointed to by self is initialized.
§Safety
The data pointed to by self must actually be initialized.
Source§impl<T: ?Sized> DataBuf<T>
impl<T: ?Sized> DataBuf<T>
Sourcepub const fn into_inner(self) -> RlManaged<T>
pub const fn into_inner(self) -> RlManaged<T>
Extract the pointer without freeing it, for the purpose of transferring ownership.
WARNING: The returned pointer must be unloaded manually to avoid a memory leak.
Source§impl<T> DataBuf<T>
impl<T> DataBuf<T>
Sourcepub fn alloc() -> Result<DataBuf<MaybeUninit<T>>, AllocationError>
pub fn alloc() -> Result<DataBuf<MaybeUninit<T>>, AllocationError>
Allocate new memory managed by Raylib.
Sourcepub fn alloc_from(val: T) -> Result<Self, (AllocationError, T)>
pub fn alloc_from(val: T) -> Result<Self, (AllocationError, T)>
Allocate new memory managed by Raylib and move val into it.
Sourcepub fn alloc_from_clone(src: &T) -> Result<Self, AllocationError>where
T: Clone,
pub fn alloc_from_clone(src: &T) -> Result<Self, AllocationError>where
T: Clone,
Allocate new memory managed by Raylib and clone val into it.
Sourcepub fn alloc_from_copy(src: &T) -> Result<Self, AllocationError>where
T: Copy,
pub fn alloc_from_copy(src: &T) -> Result<Self, AllocationError>where
T: Copy,
Allocate new memory managed by Raylib and copy val into it.
Source§impl<T> DataBuf<[T]>
impl<T> DataBuf<[T]>
Sourcepub fn alloc(count: usize) -> Result<DataBuf<[MaybeUninit<T>]>, AllocationError>
pub fn alloc(count: usize) -> Result<DataBuf<[MaybeUninit<T>]>, AllocationError>
Allocate new memory managed by Raylib.
§Example
let mut data_buf = DataBuf::<[i32]>::alloc(5).unwrap();
data_buf[0].write(4);
data_buf[1].write(8);
data_buf[2].write(-23);
data_buf[3].write(9);
data_buf[4].write(0);
// SAFETY: Just initialized all elements
let data_buf = unsafe { data_buf.assume_init() };
assert_eq!(data_buf.as_ref(), &[4, 8, -23, 9, 0]);(See also: DataBuf::alloc_from_copy)
Sourcepub fn alloc_from_clone(src: &[T]) -> Result<Self, AllocationError>where
T: Clone,
pub fn alloc_from_clone(src: &[T]) -> Result<Self, AllocationError>where
T: Clone,
Allocate memory managed by Raylib and initialize by cloning each element.
If a clone() panics partway through, the already-cloned prefix is
dropped and the allocation is freed before the panic propagates — no
leak, no drop of uninitialized memory.
§Panics
This method may panic in debug if the pointer returned by ffi::MemAlloc is unaligned.
§Example
let src = vec![String::from("a"), String::from("b")];
let data_buf = DataBuf::<[String]>::alloc_from_clone(&src).unwrap();
assert_eq!(data_buf.as_ref(), src.as_slice());Sourcepub fn alloc_from_copy(src: &[T]) -> Result<Self, AllocationError>where
T: Copy,
pub fn alloc_from_copy(src: &[T]) -> Result<Self, AllocationError>where
T: Copy,
Allocate memory managed by Raylib and initialize by copying.
§Panics
This method may panic in debug if the pointer returned by ffi::MemAlloc is unaligned.
§Example
let src = [4, 8, -23, 9, 0];
let mut data_buf = DataBuf::<[i32]>::alloc_from_copy(&src).unwrap();
assert_eq!(data_buf.as_ref(), &src);Sourcepub fn realloc(
self,
new_count: usize,
) -> Result<DataBuf<[MaybeUninit<T>]>, (AllocationError, Self)>
pub fn realloc( self, new_count: usize, ) -> Result<DataBuf<[MaybeUninit<T>]>, (AllocationError, Self)>
Reallocate memory already managed by Raylib.
§Panics
This method may panic in debug if the pointer returned by ffi::MemAlloc is unaligned.