pub trait RawVec<T> {
// Required methods
fn resize(&mut self, len: usize, additional: usize) -> bool;
fn as_ptr(&self) -> *const T;
fn as_mut_ptr(&mut self) -> *mut T;
fn try_merge<B>(
&mut self,
this_len: usize,
other: B,
other_len: usize,
) -> Result<(), B>
where B: RawVec<T>;
}
Expand description
A raw buffer allocated through an Allocator
.
§Examples
use musli::alloc::{Allocator, RawVec};
let values: [u32; 4] = [1, 2, 3, 4];
musli::alloc::default!(|alloc| {
let mut buf = alloc.new_raw_vec::<u32>();
let mut len = 0;
for value in values {
if !buf.resize(len, 1) {
panic!("Allocation failed");
}
// SAFETY: We've just resized the above buffer.
unsafe {
buf.as_mut_ptr().add(len).write(value);
}
len += 1;
}
// SAFETY: Slice does not outlive the buffer it references.
let bytes = unsafe { core::slice::from_raw_parts(buf.as_ptr(), len) };
assert_eq!(bytes, values);
});
Required Methods§
Sourcefn as_mut_ptr(&mut self) -> *mut T
fn as_mut_ptr(&mut self) -> *mut T
Get a mutable pointer into the buffer.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.