Skip to main content

ContextPtr

Struct ContextPtr 

Source
pub struct ContextPtr<T>(/* private fields */);
Expand description

Provides a safe abstraction over context that is a mutable pointer.

TODO: Not sure whether the afia-component will eventually abstract this away such that the user does not need to use it… Still feeling out this site-componenet-utils crate…

Implementations§

Source§

impl<T> ContextPtr<T>

Source

pub fn with_ref_mut<Ret>( &mut self, run: impl FnOnce(&mut T, OpaqueContextPtr<T>) -> Ret, ) -> Ret

Get a mutable reference to the value pointed to by a context pointer.

// TODO: This `afia-component` crate will eventually abstract over these `extern`
//  functions. When that happens we'll want to update this example to use the abstraction.
//  Users of this crate won't be working directly with the low-level C ABI, they'll be using
//  higher level abstractions that this crate exposes.

use afia_component::ComponentImports;
use afia_component::context::ContextPtr;

struct Context {
    imports: ComponentImports,
    label: &'static str
}

#[export_name = "__afia__$create_instance"]
pub extern "C" fn create_instance() -> *mut Context {
    Context::new_raw_with_imports_label(ComponentImports::new_dynamically_linked(), "world")
}

#[export_name = "__afia__$output$123$create_element"]
pub extern "C" fn create_element(mut context: ContextPtr<Context>, _inputs_ptr: isize, _inputs_len: usize) -> i64 {
    let div = context.with_ref_mut(|ctx, _| {
        assert_eq!(ctx.label, "world");

        let div = ctx.imports.create_element("div").unwrap();
        div.set_attribute("hello", ctx.label);

        div
    });
    div.temporary_way_to_get_i64()
}

impl Context {
    fn new_raw_with_imports_label(imports: ComponentImports, label: &'static str) -> *mut Context {
        Box::into_raw(Box::new(Context { imports, label }))
    }
}

Auto Trait Implementations§

§

impl<T> Freeze for ContextPtr<T>

§

impl<T> RefUnwindSafe for ContextPtr<T>
where T: RefUnwindSafe,

§

impl<T> !Send for ContextPtr<T>

§

impl<T> !Sync for ContextPtr<T>

§

impl<T> Unpin for ContextPtr<T>

§

impl<T> UnsafeUnpin for ContextPtr<T>

§

impl<T> UnwindSafe for ContextPtr<T>
where T: RefUnwindSafe,

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Source§

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.