pub struct RetainedCallback<R, C> { /* private fields */ }Expand description
A retained callback identifier scoped to the guest instance that created it.
The token is opaque to this crate. Its identifier type belongs to the guest implementation and should identify a callback retained by that instance.
Implementations§
Source§impl<R, C> RetainedCallback<R, C>
impl<R, C> RetainedCallback<R, C>
Sourcepub const fn new(realm: R, generation: u64, callback: C) -> Self
pub const fn new(realm: R, generation: u64, callback: C) -> Self
Create a token for a callback retained in the given realm generation.
Examples found in repository?
More examples
examples/native_host.rs (lines 33-37)
27 fn retain_callback(&mut self, value: &i64) -> Result<Callback, Self::Error> {
28 if *value != 7 || self.root.is_some() {
29 return Err("not an available callback");
30 }
31 let root = Rc::new(());
32 self.root = Some(Rc::clone(&root));
33 Ok(RetainedCallback::new(
34 Rc::clone(self.scope.realm()),
35 self.scope.generation(),
36 root,
37 ))
38 }
39
40 fn release_callback(&mut self, callback: &Callback) -> Result<(), Self::Error> {
41 if !self.current(callback) {
42 return Err("callback unavailable");
43 }
44 self.root = None;
45 Ok(())
46 }
47
48 fn register_intrinsic(
49 &mut self,
50 name: &'static str,
51 arity: u8,
52 callback: NativeIntrinsic<Self>,
53 ) -> Result<(), Self::Error> {
54 if name != "increment" || arity != 1 {
55 return Err("invalid intrinsic");
56 }
57 self.intrinsic = Some(callback);
58 Ok(())
59 }
60
61 fn invoke_retained_callback(
62 &mut self,
63 callback: &Callback,
64 arguments: &[i64],
65 ) -> Result<i64, Self::Error> {
66 if !self.current(callback) {
67 return Err("callback unavailable");
68 }
69 self.calls += 1;
70 self.intrinsic.ok_or("intrinsic unavailable")?(self, arguments)
71 }
72}
73
74impl Guest {
75 fn current(&self, callback: &Callback) -> bool {
76 callback.is_current_in(&self.scope)
77 && self
78 .root
79 .as_ref()
80 .is_some_and(|root| Rc::ptr_eq(root, callback.callback()))
81 }
82}
83
84impl NativeHost for Guest {
85 type Guest = Self;
86
87 fn guest(&self) -> &Self {
88 self
89 }
90
91 fn guest_mut(&mut self) -> &mut Self {
92 self
93 }
94}
95
96fn main() {
97 let mut guest = Guest {
98 scope: RealmScope::new(Rc::new(1), 1),
99 root: None,
100 intrinsic: None,
101 calls: 0,
102 };
103 guest
104 .register_intrinsic("increment", 1, |_, arguments| {
105 arguments
106 .first()
107 .copied()
108 .map(|value| value + 1)
109 .ok_or("missing argument")
110 })
111 .expect("register compiled intrinsic");
112 let callback = guest.retain_callback(&7).expect("retain callback");
113 assert_eq!(
114 NativeHost::invoke_retained_callback(&mut guest, &callback, &[5]),
115 Ok(6)
116 );
117 let foreign = RetainedCallback::new(Rc::new(2), 1, Rc::clone(callback.callback()));
118 assert_eq!(
119 NativeHost::invoke_retained_callback(&mut guest, &foreign, &[5]),
120 Err(NativeCallbackError::StaleCallback)
121 );
122 assert_eq!(
123 guest.calls, 1,
124 "foreign scope must not enter guest dispatch"
125 );
126 guest.release_callback(&callback).expect("release callback");
127 assert!(NativeHost::invoke_retained_callback(&mut guest, &callback.clone(), &[5]).is_err());
128 assert_eq!(guest.calls, 1, "release must revoke cloned tokens");
129 let next = guest.retain_callback(&7).expect("retain next callback");
130 guest.scope = RealmScope::new(Rc::clone(guest.scope.realm()), 2);
131 assert_eq!(
132 NativeHost::invoke_retained_callback(&mut guest, &next, &[5]),
133 Err(NativeCallbackError::StaleCallback)
134 );
135 assert_eq!(
136 guest.calls, 1,
137 "stale generation must not enter guest dispatch"
138 );
139 println!("typed native dispatch, root release, realm and generation checks passed");
140}Sourcepub const fn generation(&self) -> u64
pub const fn generation(&self) -> u64
The generation that retained this callback.
Sourcepub const fn callback(&self) -> &C
pub const fn callback(&self) -> &C
The guest-defined callback identifier.
Examples found in repository?
examples/native_host.rs (line 80)
75 fn current(&self, callback: &Callback) -> bool {
76 callback.is_current_in(&self.scope)
77 && self
78 .root
79 .as_ref()
80 .is_some_and(|root| Rc::ptr_eq(root, callback.callback()))
81 }
82}
83
84impl NativeHost for Guest {
85 type Guest = Self;
86
87 fn guest(&self) -> &Self {
88 self
89 }
90
91 fn guest_mut(&mut self) -> &mut Self {
92 self
93 }
94}
95
96fn main() {
97 let mut guest = Guest {
98 scope: RealmScope::new(Rc::new(1), 1),
99 root: None,
100 intrinsic: None,
101 calls: 0,
102 };
103 guest
104 .register_intrinsic("increment", 1, |_, arguments| {
105 arguments
106 .first()
107 .copied()
108 .map(|value| value + 1)
109 .ok_or("missing argument")
110 })
111 .expect("register compiled intrinsic");
112 let callback = guest.retain_callback(&7).expect("retain callback");
113 assert_eq!(
114 NativeHost::invoke_retained_callback(&mut guest, &callback, &[5]),
115 Ok(6)
116 );
117 let foreign = RetainedCallback::new(Rc::new(2), 1, Rc::clone(callback.callback()));
118 assert_eq!(
119 NativeHost::invoke_retained_callback(&mut guest, &foreign, &[5]),
120 Err(NativeCallbackError::StaleCallback)
121 );
122 assert_eq!(
123 guest.calls, 1,
124 "foreign scope must not enter guest dispatch"
125 );
126 guest.release_callback(&callback).expect("release callback");
127 assert!(NativeHost::invoke_retained_callback(&mut guest, &callback.clone(), &[5]).is_err());
128 assert_eq!(guest.calls, 1, "release must revoke cloned tokens");
129 let next = guest.retain_callback(&7).expect("retain next callback");
130 guest.scope = RealmScope::new(Rc::clone(guest.scope.realm()), 2);
131 assert_eq!(
132 NativeHost::invoke_retained_callback(&mut guest, &next, &[5]),
133 Err(NativeCallbackError::StaleCallback)
134 );
135 assert_eq!(
136 guest.calls, 1,
137 "stale generation must not enter guest dispatch"
138 );
139 println!("typed native dispatch, root release, realm and generation checks passed");
140}More examples
examples/native_document.rs (line 50)
49 fn release_callback(&mut self, callback: &Callback) -> Result<(), Self::Error> {
50 if !callback.is_current_in(&self.scope()) || !self.callback || *callback.callback() != 1 {
51 return Err("callback unavailable");
52 }
53 self.callback = false;
54 Ok(())
55 }
56 fn register_intrinsic(
57 &mut self,
58 name: &'static str,
59 arity: u8,
60 callback: NativeIntrinsic<Self>,
61 ) -> Result<(), Self::Error> {
62 if name != "paint" || arity != 1 {
63 return Err("unsupported intrinsic");
64 }
65 self.intrinsic = Some(callback);
66 Ok(())
67 }
68 fn invoke_retained_callback(
69 &mut self,
70 callback: &Callback,
71 args: &[i64],
72 ) -> Result<i64, Self::Error> {
73 if !callback.is_current_in(&self.scope()) || !self.callback || *callback.callback() != 1 {
74 return Err("callback unavailable");
75 }
76 self.intrinsic.ok_or("intrinsic unavailable")?(self, args)
77 }Sourcepub fn is_current_in(&self, scope: &RealmScope<R>) -> boolwhere
R: PartialEq,
pub fn is_current_in(&self, scope: &RealmScope<R>) -> boolwhere
R: PartialEq,
Whether this token belongs to the active realm generation.
Examples found in repository?
More examples
examples/native_document.rs (line 50)
49 fn release_callback(&mut self, callback: &Callback) -> Result<(), Self::Error> {
50 if !callback.is_current_in(&self.scope()) || !self.callback || *callback.callback() != 1 {
51 return Err("callback unavailable");
52 }
53 self.callback = false;
54 Ok(())
55 }
56 fn register_intrinsic(
57 &mut self,
58 name: &'static str,
59 arity: u8,
60 callback: NativeIntrinsic<Self>,
61 ) -> Result<(), Self::Error> {
62 if name != "paint" || arity != 1 {
63 return Err("unsupported intrinsic");
64 }
65 self.intrinsic = Some(callback);
66 Ok(())
67 }
68 fn invoke_retained_callback(
69 &mut self,
70 callback: &Callback,
71 args: &[i64],
72 ) -> Result<i64, Self::Error> {
73 if !callback.is_current_in(&self.scope()) || !self.callback || *callback.callback() != 1 {
74 return Err("callback unavailable");
75 }
76 self.intrinsic.ok_or("intrinsic unavailable")?(self, args)
77 }Trait Implementations§
impl<R: Copy, C: Copy> Copy for RetainedCallback<R, C>
impl<R: Eq, C: Eq> Eq for RetainedCallback<R, C>
impl<R: PartialEq, C: PartialEq> StructuralPartialEq for RetainedCallback<R, C>
Auto Trait Implementations§
impl<R, C> Forget for RetainedCallback<R, C>
impl<R, C> Freeze for RetainedCallback<R, C>
impl<R, C> Move for RetainedCallback<R, C>
impl<R, C> RefUnwindSafe for RetainedCallback<R, C>where
R: RefUnwindSafe,
C: RefUnwindSafe,
impl<R, C> Send for RetainedCallback<R, C>
impl<R, C> Sync for RetainedCallback<R, C>
impl<R, C> Unpin for RetainedCallback<R, C>
impl<R, C> UnsafeUnpin for RetainedCallback<R, C>where
R: UnsafeUnpin,
C: UnsafeUnpin,
impl<R, C> UnwindSafe for RetainedCallback<R, C>where
R: UnwindSafe,
C: UnwindSafe,
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key and return true if they are equal.impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> MaybeBoxed<Box<T>> for T
impl<T> MaybeBoxed<Box<T>> for T
Source§fn maybe_boxed(self) -> Box<T>
fn maybe_boxed(self) -> Box<T>
Convert
Source§impl<T> MaybeBoxed<T> for T
impl<T> MaybeBoxed<T> for T
Source§fn maybe_boxed(self) -> T
fn maybe_boxed(self) -> T
Convert