Skip to content

Commit 97a49c1

Browse files
committed
Some more docs
1 parent 4b92547 commit 97a49c1

File tree

2 files changed

+98
-11
lines changed

2 files changed

+98
-11
lines changed

options/src/extensions.rs

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//! Contains the [`OptionsInterface`](crate::extensions::OptionsInterface) extension interface.
2-
32
use crate::features::OptionsList;
43
use crate::{OptionsError, OptionsWriter};
54
use lv2_core::feature::Feature;
@@ -12,6 +11,100 @@ use std::panic::AssertUnwindSafe;
1211
use urid::UriBound;
1312

1413
/// An interface to allow dynamically setting options from the host.
14+
///
15+
/// # Example
16+
///
17+
///
18+
///
19+
/// ```
20+
/// # use lv2_core::prelude::*;
21+
/// # use lv2_options::{OptionType, OptionsWriter, OptionsError, Subject};
22+
/// # use lv2_options::features::OptionsList;
23+
/// # use lv2_options::extensions::{OptionsDescriptor, OptionsInterface};
24+
/// #
25+
/// # use urid::{URID, Uri};
26+
/// # use std::any::Any;
27+
/// #
28+
/// # impl<'a> OptionType<'a> for SomeIntOption {
29+
/// # type AtomType = lv2_atom::scalar::Int;
30+
/// #
31+
/// # fn from_option_value(value: &i32) -> Option<Self> {
32+
/// # Some(Self(*value))
33+
/// # }
34+
/// #
35+
/// # fn as_option_value(&'a self) -> &'a i32 {
36+
/// # &self.0
37+
/// # }
38+
/// # }
39+
/// #
40+
/// # #[derive(URIDCollection)]
41+
/// pub struct PluginUridCollection {
42+
/// some_int_option: URID<SomeIntOption>,
43+
/// int: URID<lv2_atom::scalar::Int>,
44+
/// }
45+
/// #
46+
/// # #[derive(FeatureCollection)]
47+
/// # pub struct PluginFeatures<'a> {
48+
/// # options: OptionsList<'a>,
49+
/// # }
50+
///
51+
/// # #[uri("urn:lv2_options:test:OptionablePlugin")]
52+
/// pub struct OptionablePlugin {
53+
/// some_int: SomeIntOption,
54+
/// urids: PluginUridCollection,
55+
/// }
56+
/// #
57+
/// # impl Plugin for OptionablePlugin {
58+
/// # type Ports = ();
59+
/// # type InitFeatures = PluginFeatures<'static>;
60+
/// # type AudioFeatures = ();
61+
/// #
62+
/// # fn new(_plugin_info: &PluginInfo, _features: &mut Self::InitFeatures) -> Option<Self> {
63+
/// # unimplemented!()
64+
/// # }
65+
///#
66+
/// # fn run(
67+
/// # &mut self,
68+
/// # _ports: &mut Self::Ports,
69+
/// # _features: &mut Self::AudioFeatures,
70+
/// # _sample_count: u32,
71+
/// # ) {
72+
/// # unimplemented!()
73+
/// # }
74+
/// #
75+
/// # fn extension_data(uri: &Uri) -> Option<&'static dyn Any> {
76+
/// # unimplemented!()
77+
/// # }
78+
/// # }
79+
/// #
80+
///
81+
/// #[uri("urn:lv2_options:test:SomeIntOption")]
82+
/// pub struct SomeIntOption(i32);
83+
///
84+
/// impl OptionsInterface for OptionablePlugin {
85+
/// fn get<'a>(&'a self, mut writer: OptionsWriter<'a>) -> Result<(), OptionsError> {
86+
/// writer.process(|subject, options| match subject { // We will want to get/set our opions differently depending on the subject
87+
/// Subject::Instance => { // In our case however, only our instance has an option
88+
/// options.handle(self.urids.some_int_option, self.urids.int, || {
89+
/// &self.some_int
90+
/// });
91+
/// }
92+
/// _ => {}
93+
/// })
94+
/// }
95+
///
96+
/// fn set(&mut self, options: OptionsList) -> Result<(), OptionsError> {
97+
/// options.process(|subject, options| match subject {
98+
/// Subject::Instance => {
99+
/// options.handle(self.urids.some_int_option, self.urids.int, (), |value| {
100+
/// self.some_int = value
101+
/// })
102+
/// }
103+
/// _ => {}
104+
/// })
105+
/// }
106+
/// }
107+
/// ```
15108
pub trait OptionsInterface: Plugin {
16109
/// Allows the host to retrieve the value of the given options, as currently stored by the plugin.
17110
///

options/tests/optionable.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,9 @@ pub struct PluginFeatures<'a> {
3838
options: OptionsList<'a>,
3939
}
4040

41-
pub struct PluginOptions {
42-
some_int: SomeIntOption,
43-
}
44-
4541
#[uri("urn:lv2_options:test:OptionablePlugin")]
4642
pub struct OptionablePlugin {
47-
options: PluginOptions,
43+
some_int: SomeIntOption,
4844
urids: PluginUridCollection,
4945
}
5046

@@ -55,9 +51,7 @@ impl Plugin for OptionablePlugin {
5551

5652
fn new(_plugin_info: &PluginInfo, features: &mut Self::InitFeatures) -> Option<Self> {
5753
let mut plugin = OptionablePlugin {
58-
options: PluginOptions {
59-
some_int: SomeIntOption(0),
60-
},
54+
some_int: SomeIntOption(0),
6155
urids: features.map.populate_collection()?,
6256
};
6357

@@ -86,7 +80,7 @@ impl OptionsInterface for OptionablePlugin {
8680
writer.process(|subject, options| match subject {
8781
Subject::Instance => {
8882
options.handle(self.urids.some_int_option, self.urids.int, || {
89-
&self.options.some_int
83+
&self.some_int
9084
});
9185
}
9286
_ => {}
@@ -97,7 +91,7 @@ impl OptionsInterface for OptionablePlugin {
9791
options.process(|subject, options| match subject {
9892
Subject::Instance => {
9993
options.handle(self.urids.some_int_option, self.urids.int, (), |value| {
100-
self.options.some_int = value
94+
self.some_int = value
10195
})
10296
}
10397
_ => {}

0 commit comments

Comments
 (0)