Skip to main content

StateBinding

Struct StateBinding 

Source
pub struct StateBinding<T: State> { /* private fields */ }
Expand description

Typed state binding for editors.

Wraps the get_state/set_state closures from PluginContext with typed serialization. Caches the deserialized state to avoid repeated deserialization each frame.

struct MyEditor {
    state: StateBinding<PersistentState>,
}

// In open():
self.state = StateBinding::new(&context);

// In state_changed():
self.state.sync();

// Reading:
let name = &self.state.get().instance_name;

// Writing:
self.state.update(|s| s.instance_name = new_name);

Implementations§

Source§

impl<T: State> StateBinding<T>

Source

pub fn new<P: ?Sized>(context: &PluginContext<P>) -> Self

Create a new binding from a PluginContext. Generic over the context’s <P> since StateBinding cares only about the get_state / set_state channel on the underlying EditorBridge, never about parameter typing.

Source

pub fn sync(&mut self)

Re-read state from the plugin. Call this from state_changed().

Source

pub fn get(&self) -> &T

Get the current cached state.

Source

pub fn update(&mut self, f: impl FnOnce(&mut T))

Modify state and write it back to the plugin.

Trait Implementations§

Source§

impl<T: State> Default for StateBinding<T>

Source§

fn default() -> Self

Construct an unwired binding: get() returns T::default() and update() silently discards the new state. Only useful as a placeholder before the editor is opened; replace with StateBinding::new(&context) inside Editor::open once a PluginContext is available. If you see writes vanishing, check that the binding has been wired up before you call update.

Auto Trait Implementations§

§

impl<T> Freeze for StateBinding<T>
where T: Freeze,

§

impl<T> !RefUnwindSafe for StateBinding<T>

§

impl<T> Send for StateBinding<T>
where T: Send,

§

impl<T> Sync for StateBinding<T>
where T: Sync,

§

impl<T> Unpin for StateBinding<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for StateBinding<T>
where T: UnsafeUnpin,

§

impl<T> !UnwindSafe for StateBinding<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.