#[repr(transparent)]pub struct In<'a, T>(_);
Expand description
Uninitialized stable pointer to T
.
Used for the initialize-in-place strategy employed by the View::build
method.
Implementations§
source§impl<'a, T> In<'a, T>
impl<'a, T> In<'a, T>
sourcepub unsafe fn cast<U>(self) -> In<'a, U>
pub unsafe fn cast<U>(self) -> In<'a, U>
Cast this pointer from In<T>
to In<U>
.
Safety
Caller needs to guarantee safety as per usual rules of pointer casting, namely:
T
andU
must have the same size.T
andU
must have the same memory layout.
sourcepub fn in_place<F>(self, f: F) -> Out<'a, T>where
F: FnOnce(*mut T) -> Out<'a, T>,
pub fn in_place<F>(self, f: F) -> Out<'a, T>where F: FnOnce(*mut T) -> Out<'a, T>,
Build this T
in-place using a raw pointer
Safety
This method itself is safe since just obtaining a raw pointer by itself is also safe,
it does however require unsafe code to construct Out<T>
inside the closure f
.
use kobold::internal::{In, Out};
use kobold::init;
struct Foo {
int: u32,
float: f64,
}
fn build_in(p: In<Foo>) -> Out<Foo> {
let out = p.in_place(|p| unsafe {
// Initialize fields of `Foo`
init!(p.int = 42);
init!(p.float = 3.14);
// Both fields have been initialized
Out::from_raw(p)
});
assert_eq!(out.int, 42);
assert_eq!(out.float, 3.14);
out
}
sourcepub unsafe fn raw<F>(raw: *mut T, f: F) -> Out<'a, T>where
F: FnOnce(In<'_, T>) -> Out<'_, T>,
pub unsafe fn raw<F>(raw: *mut T, f: F) -> Out<'a, T>where F: FnOnce(In<'_, T>) -> Out<'_, T>,
Initialize raw pointer raw
using a builder closure f
.
Safety
Caller must guarantee that raw
is a stable pointer for the entire life of T
.
If raw
has already been initialized this can cause a memory leak, which is safe but undesirable.
sourcepub fn pinned<F>(pin: Pin<&'a mut MaybeUninit<T>>, f: F) -> Out<'a, T>where
F: FnOnce(In<'_, T>) -> Out<'_, T>,
pub fn pinned<F>(pin: Pin<&'a mut MaybeUninit<T>>, f: F) -> Out<'a, T>where F: FnOnce(In<'_, T>) -> Out<'_, T>,
Initialize a pinned uninitialized data using a builder closure f
.
Safety
This method is safe, it can however leak memory if pin
has already been initialized.