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>
impl<T> ContextPtr<T>
Sourcepub fn with_ref_mut<Ret>(
&mut self,
run: impl FnOnce(&mut T, OpaqueContextPtr<T>) -> Ret,
) -> Ret
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more