Skip to content

Commit 49c7f4f

Browse files
Merge pull request #85 from Janonard/develop
Making URIDs generic for all functions
2 parents 0f66c79 + 782175c commit 49c7f4f

File tree

3 files changed

+46
-25
lines changed

3 files changed

+46
-25
lines changed

atom/src/object.rs

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
//! ).unwrap();
6262
//!
6363
//! // Write a property to the object.
64-
//! object_writer.init(urids.property_a, None, urids.atom.int, 42).unwrap();
64+
//! object_writer.init(urids.property_a, urids.atom.int, 42).unwrap();
6565
//! }
6666
//! ```
6767
//!
@@ -190,17 +190,32 @@ pub struct ObjectWriter<'a, 'b> {
190190
}
191191

192192
impl<'a, 'b> ObjectWriter<'a, 'b> {
193+
/// Initialize a new property with a context.
194+
///
195+
/// This method does the same as [`init`](#method.init), but also sets the context URID.
196+
pub fn init_with_context<'c, K: ?Sized, T: ?Sized, A: Atom<'a, 'c>>(
197+
&'c mut self,
198+
key: URID<K>,
199+
context: URID<T>,
200+
child_urid: URID<A>,
201+
parameter: A::WriteParameter,
202+
) -> Option<A::WriteHandle> {
203+
Property::write_header(&mut self.frame, key.into_general(), Some(context))?;
204+
(&mut self.frame as &mut dyn MutSpace).init(child_urid, parameter)
205+
}
206+
193207
/// Initialize a new property.
194208
///
195209
/// This method writes out the header of a property and returns a reference to the space, so the property values can be written.
196-
pub fn init<'c, G: ?Sized, A: Atom<'a, 'c>>(
210+
///
211+
/// Properties also have a context URID internally, which is rarely used. If you want to add one, use [`init_with_context`](#method.init_with_context).
212+
pub fn init<'c, K: ?Sized, A: Atom<'a, 'c>>(
197213
&'c mut self,
198-
key: URID<G>,
199-
context: Option<URID>,
214+
key: URID<K>,
200215
child_urid: URID<A>,
201216
parameter: A::WriteParameter,
202217
) -> Option<A::WriteHandle> {
203-
Property::write_header(&mut self.frame, key.into_general(), context)?;
218+
Property::write_header::<K, ()>(&mut self.frame, key, None)?;
204219
(&mut self.frame as &mut dyn MutSpace).init(child_urid, parameter)
205220
}
206221
}
@@ -254,7 +269,11 @@ impl Property {
254269
/// Write out the header of a property atom.
255270
///
256271
/// This method simply writes out the content of the header to the space and returns `Some(())` if it's successful.
257-
fn write_header(space: &mut dyn MutSpace, key: URID, context: Option<URID>) -> Option<()> {
272+
fn write_header<K: ?Sized, C: ?Sized>(
273+
space: &mut dyn MutSpace,
274+
key: URID<K>,
275+
context: Option<URID<C>>,
276+
) -> Option<()> {
258277
space.write(&key.get(), true)?;
259278
space.write(&context.map(|urid| urid.get()).unwrap_or(0), false)?;
260279
Some(())
@@ -302,14 +321,10 @@ mod tests {
302321
)
303322
.unwrap();
304323
{
305-
writer
306-
.init(first_key, None, urids.int, first_value)
307-
.unwrap();
324+
writer.init(first_key, urids.int, first_value).unwrap();
308325
}
309326
{
310-
writer
311-
.init(second_key, None, urids.float, second_value)
312-
.unwrap();
327+
writer.init(second_key, urids.float, second_value).unwrap();
313328
}
314329
}
315330

state/src/raw.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ impl<'a> StoreHandle<'a> {
3434
/// This will return a new handle to create a property. Once the property is completely written, you can commit it by calling [`commit`](#method.commit) or [`commit_all`](#method.commit_all). Then, and only then, it will be saved by the host.
3535
///
3636
/// If you began to write a property and don't want the written things to be stored, you can discard it with [`discard`](#method.discard) or [`discard_all`](#method.discard_all).
37-
pub fn draft(&mut self, property_key: URID) -> StatePropertyWriter {
37+
pub fn draft<K: ?Sized>(&mut self, property_key: URID<K>) -> StatePropertyWriter {
38+
let property_key = property_key.into_general();
3839
self.properties
3940
.insert(property_key, SpaceElement::default());
4041
StatePropertyWriter::new(SpaceHead::new(
@@ -43,10 +44,10 @@ impl<'a> StoreHandle<'a> {
4344
}
4445

4546
/// Internal helper function to store a property.
46-
pub fn commit_pair(
47+
pub fn commit_pair<K: ?Sized>(
4748
store_fn: sys::LV2_State_Store_Function,
4849
handle: sys::LV2_State_Handle,
49-
key: URID,
50+
key: URID<K>,
5051
space: SpaceElement,
5152
) -> Result<(), StateErr> {
5253
let store_fn = store_fn.ok_or(StateErr::BadCallback)?;
@@ -83,7 +84,8 @@ impl<'a> StoreHandle<'a> {
8384
/// Commit one specific property.
8485
///
8586
/// This method returns `None` if the requested property was not marked for commit, `Some(Ok(()))` if the property was stored and `Some(Err(_))` if an error occured while storing the property.
86-
pub fn commit(&mut self, key: URID) -> Option<Result<(), StateErr>> {
87+
pub fn commit<K: ?Sized>(&mut self, key: URID<K>) -> Option<Result<(), StateErr>> {
88+
let key = key.into_general();
8789
let space = self.properties.remove(&key)?;
8890
Some(Self::commit_pair(self.store_fn, self.handle, key, space))
8991
}
@@ -96,8 +98,8 @@ impl<'a> StoreHandle<'a> {
9698
/// Discard a drafted property.
9799
///
98100
/// If no property with the given key was drafted before, this is a no-op.
99-
pub fn discard(&mut self, key: URID) {
100-
self.properties.remove(&key);
101+
pub fn discard<K: ?Sized>(&mut self, key: URID<K>) {
102+
self.properties.remove(&key.into_general());
101103
}
102104
}
103105

@@ -158,7 +160,7 @@ impl<'a> RetrieveHandle<'a> {
158160
/// Try to retrieve a property from the host.
159161
///
160162
/// This method calls the internal retrieve callback with the given URID. If there's no property with the given URID, `Err(StateErr::NoProperty)` is returned. Otherwise, a reading handle is returned that contains the type and the data of the property and can interpret it as an atom.
161-
pub fn retrieve(&self, key: URID) -> Result<StatePropertyReader, StateErr> {
163+
pub fn retrieve<K: ?Sized>(&self, key: URID<K>) -> Result<StatePropertyReader, StateErr> {
162164
let mut size: usize = 0;
163165
let mut type_: u32 = 0;
164166
let property_ptr: *const std::ffi::c_void = unsafe {
@@ -192,8 +194,11 @@ pub struct StatePropertyReader<'a> {
192194

193195
impl<'a> StatePropertyReader<'a> {
194196
/// Create a new reading handle with the given type and data.
195-
pub fn new(type_: URID, body: Space<'a>) -> Self {
196-
Self { type_, body }
197+
pub fn new<T: ?Sized>(type_: URID<T>, body: Space<'a>) -> Self {
198+
Self {
199+
type_: type_.into_general(),
200+
body,
201+
}
197202
}
198203

199204
/// Return the type of the property.

state/src/storage.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ impl Default for Storage {
2323

2424
impl Storage {
2525
/// Store a property.
26-
pub fn store(&mut self, key: URID, type_: URID, value: &[u8]) {
27-
self.items.insert(key, (type_, value.to_owned()));
26+
pub fn store<K: ?Sized, T: ?Sized>(&mut self, key: URID<K>, type_: URID<T>, value: &[u8]) {
27+
self.items
28+
.insert(key.into_general(), (type_.into_general(), value.to_owned()));
2829
}
2930

3031
/// External version of [`store`](#method.store).
@@ -60,9 +61,9 @@ impl Storage {
6061
/// Try to retrieve a property.
6162
///
6263
/// If the property doesn't exist, `None` is returned.
63-
pub fn retrieve(&self, key: URID) -> Option<(URID, &[u8])> {
64+
pub fn retrieve<K: ?Sized>(&self, key: URID<K>) -> Option<(URID, &[u8])> {
6465
self.items
65-
.get(&key)
66+
.get(&key.into_general())
6667
.map(|(urid, data)| (*urid, data.as_ref()))
6768
}
6869

0 commit comments

Comments
 (0)