Struct ExtState

Source
pub struct ExtState<'a, T: Serialize + DeserializeOwned + Clone + Debug, O: HasExtState> { /* private fields */ }
Expand description

Serializes extension data.

This struct should be used instead of simple set_ext_state and get_ext_state calls.

The features, that make it better are:

  • It serializes not strings, but anything, can be serialized by serde crate.
  • It provides the similar interface as for global ext state values, as well as to project or other objects ext data. Currently supported: Project, Track, TrackSend, Envelope, Item, Take
  • it erases data, if in process of development you decided to turn the persistence off.
  • it can be initialized with value, but only once, if persistence is needed.

Also, it is very idiomatic and type-safe, so it ideally suits as gui state etc.

§Note

Be careful with ext section and key. If two different states of different types are saved for one key — it will panic at get().

§Usage

use rea_rs::{ExtState, HasExtState, Reaper, Project};
let rpr = Reaper::get();
let mut state =
    ExtState::new("test section", "first", Some(10), true, rpr);
assert_eq!(state.get().expect("can not get value"), 10);
state.set(56);
assert_eq!(state.get().expect("can not get value"), 56);
state.delete();
assert!(state.get().is_none());

let mut pr = rpr.current_project();
let mut state: ExtState<u32, Project> =
    ExtState::new("test section", "first", None, true, &pr);
assert_eq!(state.get().expect("can not get value"), 10);
state.set(56);
assert_eq!(state.get().expect("can not get value"), 56);
state.delete();
assert!(state.get().is_none());

let tr = pr.get_track_mut(0).unwrap();
let mut state = ExtState::new("testsection", "first", 45, false, &tr);
assert_eq!(state.get().expect("can not get value"), 45);
state.set(15);
assert_eq!(state.get().expect("can not get value"), 15);
state.delete();
assert_eq!(state.get(), None);

Implementations§

Source§

impl<'a, T: Serialize + DeserializeOwned + Clone + Debug, O: HasExtState> ExtState<'a, T, O>

Source

pub fn new( section: impl Into<String>, key: impl Into<String>, value: impl Into<Option<T>>, persist: bool, object: &'a O, ) -> Self

Create ext state object.

If Some(value) provided, but persist is true, only first call to the ExtState::new will initialize it. Later will keep previous value.

Source

pub fn get(&self) -> Option<T>

Get value from ext state.

Returns None if no value saved in REAPER.

§Panics

If value of the wrong type stored in the same section/key.

Source

pub fn set(&mut self, value: T)

Set the value to ext state.

Source

pub fn delete(&mut self)

Erase ext value, but keep the object.

Trait Implementations§

Source§

impl<'a, T: Debug + Serialize + DeserializeOwned + Clone + Debug, O: Debug + HasExtState> Debug for ExtState<'a, T, O>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a, T: PartialEq + Serialize + DeserializeOwned + Clone + Debug, O: PartialEq + HasExtState> PartialEq for ExtState<'a, T, O>

Source§

fn eq(&self, other: &ExtState<'a, T, O>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a, T: Serialize + DeserializeOwned + Clone + Debug, O: HasExtState> StructuralPartialEq for ExtState<'a, T, O>

Auto Trait Implementations§

§

impl<'a, T, O> Freeze for ExtState<'a, T, O>
where T: Freeze,

§

impl<'a, T, O> RefUnwindSafe for ExtState<'a, T, O>

§

impl<'a, T, O> Send for ExtState<'a, T, O>
where T: Send, O: Sync,

§

impl<'a, T, O> Sync for ExtState<'a, T, O>
where T: Sync, O: Sync,

§

impl<'a, T, O> Unpin for ExtState<'a, T, O>
where T: Unpin,

§

impl<'a, T, O> UnwindSafe for ExtState<'a, T, O>

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> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
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.