Skip to content

Commit

Permalink
Bound event listeners should carry the lifetime of the Hook (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
maciejhirsz authored May 26, 2023
1 parent 447b65c commit 3178c41
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions crates/kobold/src/stateful/hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl<S> Hook<S> {
F: Fn(&mut S, E) -> O + 'static,
O: ShouldRender,
{
let inner = &self.inner as *const Inner<S>;
let inner = &self.inner;

Bound { inner, callback }
}
Expand Down Expand Up @@ -144,12 +144,12 @@ impl<S> Hook<S> {
}
}

pub struct Bound<S, F> {
inner: *const Inner<S>,
pub struct Bound<'b, S, F> {
inner: &'b Inner<S>,
callback: F,
}

impl<S, F> Bound<S, F> {
impl<S, F> Bound<'_, S, F> {
pub fn into_listener<E, O>(self) -> impl Listener<E>
where
S: 'static,
Expand All @@ -159,6 +159,7 @@ impl<S, F> Bound<S, F> {
{
let Bound { inner, callback } = self;

let inner = inner as *const Inner<S>;
let bound = move |e| {
// ⚠️ Safety:
// ==========
Expand All @@ -181,7 +182,7 @@ impl<S, F> Bound<S, F> {
}
}

impl<S, F> Clone for Bound<S, F>
impl<S, F> Clone for Bound<'_, S, F>
where
F: Clone,
{
Expand All @@ -193,7 +194,7 @@ where
}
}

impl<S, F> Copy for Bound<S, F> where F: Copy {}
impl<S, F> Copy for Bound<'_, S, F> where F: Copy {}

struct BoundListener<B, U> {
bound: B,
Expand Down Expand Up @@ -275,7 +276,7 @@ mod test {
};

let mock = Bound {
inner: &inner as *const _,
inner: &inner,
callback: |state: &mut i32, _: web_sys::Event| {
*state += 1;
},
Expand Down

0 comments on commit 3178c41

Please sign in to comment.