Skip to content

Commit a6d22fd

Browse files
committed
Post-merge fixes
1 parent e19ffbe commit a6d22fd

File tree

8 files changed

+51
-42
lines changed

8 files changed

+51
-42
lines changed

atom/src/atoms/scalar.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ impl<A: ScalarAtom> Atom for A {
9292
}
9393
}
9494

95-
impl<A: ScalarAtom> BackAsSpace for A {
96-
fn back_as_space<'a>(handle: <Self::ReadHandle as AtomHandle<'a>>::Handle) -> &'a [u8] {
95+
impl<A: ScalarAtom> AtomAsBytes for A {
96+
fn read_as_bytes<'a>(handle: <Self::ReadHandle as AtomHandle<'a>>::Handle) -> &'a [u8] {
9797
AlignedSpace::from_slice(::core::slice::from_ref(handle)).as_bytes()
9898
}
9999
}

atom/src/lib.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,28 @@ pub trait Atom: UriBound {
164164
writer: AtomWriter,
165165
) -> Result<<Self::WriteHandle as AtomHandle>::Handle, AtomWriteError>;
166166
}
167+
168+
/// An Atom super-trait that allows to get a byte slice from an atom's read handle.
169+
///
170+
/// Some LV2 APIs (such as `Option`) request a data pointer to the value of a given atom type, but
171+
/// in many cases that pointer can be simply retrieved from a reference to a raw value. Most notably,
172+
/// pointers to any scalar value (e.g. `&i32`) can be safely turned into a byte slice (`&[u8]).
173+
///
174+
/// However, not all atoms have this capability, hence the need for a separate trait that is not
175+
/// implemented for all types.
176+
///
177+
/// # Example
178+
///
179+
/// ```
180+
/// use lv2_atom::atoms::scalar::Int;
181+
/// use lv2_atom::AtomAsBytes;
182+
///
183+
/// let value: i32 = 42;
184+
/// let bytes: &[u8] = Int::read_as_bytes(&value);
185+
///
186+
/// assert_eq!(bytes.len(), ::core::mem::size_of::<i32>())
187+
/// ```
188+
pub trait AtomAsBytes: Atom {
189+
/// Returns the type returned by an Atom's read handle as a byte slice.
190+
fn read_as_bytes<'a>(handle: <Self::ReadHandle as AtomHandle<'a>>::Handle) -> &'a [u8];
191+
}

options/src/collection.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ pub mod __implementation {
176176
Some(value) => self.inner.deserialize_to(value, options),
177177
None => match self.inner.deserialize_new(options) {
178178
Ok(v) => {
179-
destination.insert(v);
179+
let _ = destination.insert(v);
180180
Ok(())
181181
}
182182
Err(e) => Err(e),

options/src/extensions.rs

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,21 @@ use urid::UriBound;
2424
/// #
2525
/// # use urid::{URID, Uri, URIDCollection, uri, Map, UriBound};
2626
/// # use std::any::Any;
27+
/// # use lv2_atom::atoms::scalar::Int;
28+
/// use lv2_options::collection::OptionsSerializer;
2729
/// #
28-
/// # impl<'a> OptionType<'a> for SomeIntOption {
29-
/// # type AtomType = lv2_atom::scalar::Int;
30+
/// # impl OptionType for SomeIntOption {
31+
/// # type AtomType = Int;
3032
/// #
3133
/// # fn from_option_value(value: &i32) -> Option<Self> {
3234
/// # Some(Self(*value))
3335
/// # }
3436
/// #
35-
/// # fn as_option_value(&'a self) -> &'a i32 {
37+
/// # fn as_option_value(&self) -> &i32 {
3638
/// # &self.0
3739
/// # }
3840
/// # }
3941
/// #
40-
/// # #[derive(URIDCollection)]
41-
/// pub struct PluginUridCollection {
42-
/// some_int_option: URID<SomeIntOption>,
43-
/// int: URID<lv2_atom::scalar::Int>,
44-
/// }
45-
/// #
4642
/// # #[derive(FeatureCollection)]
4743
/// # pub struct PluginFeatures<'a> {
4844
/// # options: OptionsList<'a>,
@@ -51,7 +47,7 @@ use urid::UriBound;
5147
/// # #[uri("urn:lv2_options:test:OptionablePlugin")]
5248
/// pub struct OptionablePlugin {
5349
/// some_int: SomeIntOption,
54-
/// urids: PluginUridCollection,
50+
/// some_int_serializer: OptionsSerializer<SomeIntOption>,
5551
/// }
5652
/// #
5753
/// # impl Plugin for OptionablePlugin {
@@ -82,26 +78,12 @@ use urid::UriBound;
8278
/// pub struct SomeIntOption(i32);
8379
///
8480
/// 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-
/// })
81+
/// fn get<'a>(&'a self, mut requests: OptionRequestList<'a>) -> Result<(), OptionsError> {
82+
/// self.some_int_serializer.respond_to_requests(&self.some_int, &mut requests)
9483
/// }
9584
///
9685
/// 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-
/// })
86+
/// self.some_int_serializer.deserialize_to(&mut self.some_int, &options)
10587
/// }
10688
/// }
10789
/// ```

options/src/lib.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@ pub mod features {
3434

3535
/// Prelude of `lv2_options` for wildcard usage.
3636
pub mod prelude {
37-
pub use crate::extensions::{OptionsDescriptor, OptionsInterface};
38-
pub use crate::list::OptionsList;
39-
pub use crate::OptionsError;
40-
pub use crate::Subject;
37+
pub use crate::{
38+
extensions::{OptionsDescriptor, OptionsInterface},
39+
list::OptionsList,
40+
request::{OptionRequest, OptionRequestList},
41+
OptionsError, Subject,
42+
};
4143
}

options/src/option.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use lv2_atom::{Atom, AtomHandle, BackAsSpace};
1+
use lv2_atom::{Atom, AtomAsBytes, AtomHandle};
22
use urid::UriBound;
33

44
pub mod error;
@@ -20,7 +20,7 @@ pub mod value;
2020
/// pub struct SomeIntOption(i32);
2121
///
2222
/// impl OptionType for SomeIntOption {
23-
/// type AtomType = lv2_atom::scalar::Int;
23+
/// type AtomType = lv2_atom::atoms::scalar::Int;
2424
///
2525
/// fn from_option_value(value: &i32) -> Option<Self> {
2626
/// Some(Self(*value))
@@ -32,7 +32,7 @@ pub mod value;
3232
/// }
3333
/// ```
3434
pub trait OptionType: UriBound + Sized {
35-
type AtomType: BackAsSpace;
35+
type AtomType: AtomAsBytes;
3636

3737
/// Creates a new instance of this Option type from a given atom value.
3838
///

options/src/option/request.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{OptionType, OptionValue, OptionsError, Subject};
2-
use lv2_atom::{Atom, AtomHandle, BackAsSpace};
2+
use lv2_atom::{Atom, AtomAsBytes, AtomHandle};
33
use std::marker::PhantomData;
44
use urid::URID;
55

@@ -45,7 +45,7 @@ impl<'a> OptionRequest<'a> {
4545
value: &'a T,
4646
) -> Result<(), OptionsError>
4747
where
48-
T::AtomType: BackAsSpace,
48+
T::AtomType: AtomAsBytes,
4949
{
5050
if !self.is(option_type) {
5151
return Err(OptionsError::BadKey);
@@ -62,9 +62,9 @@ impl<'a> OptionRequest<'a> {
6262
value_type: URID<T>,
6363
value_handle: <<T as Atom>::ReadHandle as AtomHandle>::Handle,
6464
) where
65-
T: BackAsSpace,
65+
T: AtomAsBytes,
6666
{
67-
let data = T::back_as_space(value_handle);
67+
let data = T::read_as_bytes(value_handle);
6868
self.inner.type_ = value_type.get();
6969
self.inner.size = data.len() as u32;
7070
self.inner.value = data.as_ptr().cast();

options/src/option/value.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,6 @@ impl OptionValue {
6464
&self,
6565
) -> Option<<<T as Atom>::ReadHandle as AtomHandle>::Handle> {
6666
// TODO: Atoms can actually be from non-aligned spaces
67-
T::read(AtomSpace::from_bytes_unchecked(self.data()?))
67+
T::read(AtomSpace::from_bytes_unchecked(self.data()?)).ok()
6868
}
6969
}

0 commit comments

Comments
 (0)