pub struct ScalarLocalState<T: Send + 'static> { /* private fields */ }Expand description
Type-safe per-thread local state for a DuckDB scalar function.
DuckDB calls the init callback once per execution thread, so each thread
gets its own T and get_mut hands out &mut T without
synchronisation. That is why T need only be Send, not Sync — and why a
counter kept here is per-thread, not global. It must be Send because the
thread that frees it need not be the one that created it.
Implementations§
Source§impl<T: Send + 'static> ScalarLocalState<T>
impl<T: Send + 'static> ScalarLocalState<T>
Sourcepub fn set(info: &ScalarInitInfo, state: T)
pub fn set(info: &ScalarInitInfo, state: T)
Stores state as this thread’s local state.
Call at most once per init invocation: DuckDB overwrites the stored
pointer on a second call without freeing the first value, so the
first value is leaked (never dropped). The C API offers no way to read
the state back from an init callback, so quack-rs cannot free it.
Sourcepub unsafe fn get_mut<'a>(info: &ScalarFunctionInfo) -> Option<&'a mut T>
pub unsafe fn get_mut<'a>(info: &ScalarFunctionInfo) -> Option<&'a mut T>
Mutably borrows this thread’s local state during execution.
Returns None if the init callback never stored anything.
§Safety
- The init callback must have stored a
Tviaset. - Only one borrow may be live at a time within a callback invocation.
- The returned reference must not outlive the callback invocation that
infobelongs to. The lifetime'ais the caller’s to choose and nothing ties it toinfo;DuckDBfrees the local state when the thread’s execution ends.