Debian Codegraph @

helix-event/src/hook.rs

raw symbols SCIP
1 //! rust dynamic dispatch is extremely limited so we have to build our
2 //! own vtable implementation. Otherwise implementing the event system would not be possible.
3 //! A nice bonus of this approach is that we can optimize the vtable a bit more. Normally
4 //! a dyn Trait fat pointer contains two pointers: A pointer to the data itself and a
5 //! pointer to a global (static) vtable entry which itself contains multiple other pointers
6 //! (the various functions of the trait, drop, size and align). That makes dynamic
7 //! dispatch pretty slow (double pointer indirections). However, we only have a single function
8 //! in the hook trait and don't need a drop implementation (event system is global anyway
9 //! and never dropped) so we can just store the entire vtable inline.
10
11 use anyhow::Result;
12 use std::ptr::{self, NonNull};
13
14 use crate::Event;
15
16 /// Opaque handle type that represents an erased type parameter.
17 ///
18 /// If extern types were stable, this could be implemented as `extern { pub type Opaque; }` but
19 /// until then we can use this.
20 ///
21 /// Care should be taken that we don't use a concrete instance of this. It should only be used
22 /// through a reference, so we can maintain something else's lifetime.
23 struct Opaque(());
24
25 pub(crate) struct ErasedHook {
26 data: NonNull<Opaque>,
27 call: unsafe fn(NonNull<Opaque>, NonNull<Opaque>, NonNull<Opaque>),
28 }
29
30 impl ErasedHook {
31 pub(crate) fn new_dynamic<H: Fn() -> Result<()> + 'static + Send + Sync>(
32 hook: H,
33 ) -> ErasedHook {
34 unsafe fn call<F: Fn() -> Result<()> + 'static + Send + Sync>(
35 hook: NonNull<Opaque>,
36 _event: NonNull<Opaque>,
37 result: NonNull<Opaque>,
38 ) {
39 let hook: NonNull<F> = hook.cast();
40 let result: NonNull<Result<()>> = result.cast();
41 let hook: &F = hook.as_ref();
42 let res = hook();
43 ptr::write(result.as_ptr(), res)
44 }
45
46 unsafe {
47 ErasedHook {
48 data: NonNull::new_unchecked(Box::into_raw(Box::new(hook)) as *mut Opaque),
49 call: call::<H>,
50 }
51 }
52 }
53
54 pub(crate) fn new<E: Event, F: Fn(&mut E) -> Result<()>>(hook: F) -> ErasedHook {
55 unsafe fn call<E: Event, F: Fn(&mut E) -> Result<()>>(
56 hook: NonNull<Opaque>,
57 event: NonNull<Opaque>,
58 result: NonNull<Opaque>,
59 ) {
60 let hook: NonNull<F> = hook.cast();
61 let mut event: NonNull<E> = event.cast();
62 let result: NonNull<Result<()>> = result.cast();
63 let hook: &F = hook.as_ref();
64 let res = hook(event.as_mut());
65 ptr::write(result.as_ptr(), res)
66 }
67
68 unsafe {
69 ErasedHook {
70 data: NonNull::new_unchecked(Box::into_raw(Box::new(hook)) as *mut Opaque),
71 call: call::<E, F>,
72 }
73 }
74 }
75
76 pub(crate) unsafe fn call<E: Event>(&self, event: &mut E) -> Result<()> {
77 let mut res = Ok(());
78
79 unsafe {
80 (self.call)(
81 self.data,
82 NonNull::from(event).cast(),
83 NonNull::from(&mut res).cast(),
84 );
85 }
86 res
87 }
88 }
89
90 unsafe impl Sync for ErasedHook {}
91 unsafe impl Send for ErasedHook {}