Struct kobold::internal::In

source ·
#[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>

source

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:

  1. T and U must have the same size.
  2. T and U must have the same memory layout.
source

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
}
source

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.

source

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.

source

pub fn replace<F>(at: &mut T, f: F) -> Twhere F: FnOnce(In<'_, T>) -> Out<'_, T>,

Replace previous value of T with a new value produced by a builder closure f. Returns the old value.

source

pub fn put(self, val: T) -> Out<'a, T>

Initialize this pointer with some value of T.

Auto Trait Implementations§

§

impl<'a, T> RefUnwindSafe for In<'a, T>where T: RefUnwindSafe,

§

impl<'a, T> Send for In<'a, T>where T: Send,

§

impl<'a, T> Sync for In<'a, T>where T: Sync,

§

impl<'a, T> Unpin for In<'a, T>

§

impl<'a, T> !UnwindSafe for In<'a, T>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.