Skip to content

Commit 4a80d43

Browse files
committed
More WIP, still not working
1 parent efbd941 commit 4a80d43

File tree

16 files changed

+216
-131
lines changed

16 files changed

+216
-131
lines changed

atom/src/atoms.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub struct AtomURIDCollection {
3030
}
3131

3232
impl AtomURIDCollection {
33-
pub fn vector<S: scalar::ScalarAtom>(&self) -> URID<vector::Vector<S>> {
33+
pub fn vector<'a, 'b: 'a, S: scalar::ScalarAtom<'a, 'b>>(&self) -> URID<vector::Vector<S>> {
3434
unsafe { URID::new_unchecked(self.vector.get()) }
3535
}
3636
}

atom/src/atoms/object.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -411,11 +411,11 @@ mod tests {
411411

412412
let (header, atom) = properties[0];
413413
assert_eq!(header.key, first_key);
414-
assert_eq!(atom.read(urids.int, ()).unwrap(), first_value);
414+
assert_eq!(*atom.read(urids.int, ()).unwrap(), first_value);
415415

416416
let (header, atom) = properties[1];
417417
assert_eq!(header.key, second_key);
418-
assert_eq!(atom.read(urids.float, ()).unwrap(), second_value);
418+
assert_eq!(*atom.read(urids.float, ()).unwrap(), second_value);
419419
}
420420
}
421421
}

atom/src/atoms/scalar.rs

+52-36
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
//! /// Something like a plugin's run method.
2020
//! fn run(ports: &mut MyPorts, urids: &AtomURIDCollection) {
2121
//! // Scalar atoms don't need a reading parameter.
22-
//! let read_value: f32 = ports.input.read(urids.float, ()).unwrap();
22+
//! let read_value: &f32 = ports.input.read(urids.float, ()).unwrap();
2323
//!
2424
//! // Writing is done with the value of the atom.
2525
//! ports.output.init(urids.float, 17.0).unwrap();
@@ -37,7 +37,16 @@ use urid::URID;
3737
/// An atom that only contains a single, scalar value.
3838
///
3939
/// Since scalar values are so simple, the reading and writing methods are exactly the same.
40-
pub trait ScalarAtom: UriBound {
40+
pub trait ScalarAtom<'handle, 'space: 'handle>:
41+
Atom<
42+
'handle,
43+
'space,
44+
ReadParameter = (),
45+
ReadHandle = &'handle Self::InternalType,
46+
WriteParameter = Self::InternalType,
47+
WriteHandle = (),
48+
> + UriBound
49+
{
4150
/// The internal representation of the atom.
4251
///
4352
/// For example, the `Int` atom has the internal type of `i32`, which is `i32` on most platforms.
@@ -47,15 +56,15 @@ pub trait ScalarAtom: UriBound {
4756
///
4857
/// If the space does not contain the atom or is not big enough, return `None`. The second return value is the space behind the atom.
4958
#[inline]
50-
unsafe fn read_scalar(body: &AtomSpace) -> Option<Self::InternalType> {
51-
body.read().next_value().copied()
59+
unsafe fn read_scalar(body: &AtomSpace) -> Option<&Self::InternalType> {
60+
body.read().next_value()
5261
}
5362

5463
/// Try to write the atom into a space.
5564
///
5665
/// Write an atom with the value of `value` into the space and return a mutable reference to the written value. If the space is not big enough, return `None`.
5766
#[inline]
58-
fn write_scalar<'handle, 'space: 'handle>(
67+
fn write_scalar(
5968
mut frame: AtomSpaceWriter<'handle, 'space>,
6069
value: Self::InternalType,
6170
) -> Option<()> {
@@ -66,31 +75,38 @@ pub trait ScalarAtom: UriBound {
6675
}
6776
}
6877

69-
impl<'handle, 'space: 'handle, A: ScalarAtom> Atom<'handle, 'space> for A {
70-
type ReadParameter = ();
71-
type ReadHandle = A::InternalType;
72-
type WriteParameter = A::InternalType;
73-
type WriteHandle = ();
74-
75-
unsafe fn read(body: &'space AtomSpace, _: ()) -> Option<A::InternalType> {
76-
<A as ScalarAtom>::read_scalar(body)
77-
}
78-
79-
fn init(frame: AtomSpaceWriter<'handle, 'space>, value: A::InternalType) -> Option<()> {
80-
<A as ScalarAtom>::write_scalar(frame, value)
81-
}
82-
}
83-
8478
/// Macro to atomate the definition of scalar atoms.
8579
macro_rules! make_scalar_atom {
8680
($atom:ty, $internal:ty, $uri:expr, $urid:expr) => {
8781
unsafe impl UriBound for $atom {
8882
const URI: &'static [u8] = $uri;
8983
}
9084

91-
impl ScalarAtom for $atom {
85+
impl<'handle, 'space: 'handle> ScalarAtom<'handle, 'space> for $atom {
9286
type InternalType = $internal;
9387
}
88+
89+
impl<'handle, 'space: 'handle> Atom<'handle, 'space> for $atom {
90+
type ReadParameter = ();
91+
type ReadHandle = &'handle $internal;
92+
type WriteParameter = $internal;
93+
type WriteHandle = ();
94+
95+
unsafe fn read(body: &'space AtomSpace, _: ()) -> Option<&'handle $internal> {
96+
<Self as ScalarAtom>::read_scalar(body)
97+
}
98+
99+
fn init(frame: AtomSpaceWriter<'handle, 'space>, value: $internal) -> Option<()> {
100+
<Self as ScalarAtom>::write_scalar(frame, value)
101+
}
102+
}
103+
104+
impl<'handle, 'space: 'handle> BackAsSpace<'handle, 'space> for $atom {
105+
fn back_as_space(value: &'handle $internal) -> &'handle AlignedSpace<u8> {
106+
let bytes = AlignedSpace::from_slice(::core::slice::from_ref(value)).as_bytes();
107+
AlignedSpace::from_slice(bytes)
108+
}
109+
}
94110
};
95111
}
96112

@@ -165,33 +181,33 @@ mod tests {
165181
use std::convert::TryFrom;
166182
use urid::*;
167183

168-
fn test_scalar<A: ScalarAtom>(value: A::InternalType)
184+
fn test_scalar<A, T>(value: T)
169185
where
170-
A::InternalType: PartialEq<A::InternalType>,
171-
A::InternalType: std::fmt::Debug,
186+
for<'space> A: ScalarAtom<'space, 'space, InternalType = T>,
187+
T: PartialEq<T>,
188+
T: std::fmt::Debug,
172189
{
173190
let map = HashURIDMapper::new();
174191
let urid: URID<A> = map.map_type().unwrap();
175192

176193
let mut raw_space = VecSpace::<AtomHeader>::new_with_capacity(64);
177-
let raw_space = raw_space.as_space_mut();
178194

179195
// writing
180196
{
181197
let mut space = SpaceCursor::new(raw_space.as_bytes_mut());
182-
space.init_atom(urid, value).unwrap();
198+
let foo = space.init_atom(urid, value).unwrap();
183199
}
184200

185201
// verifying
186-
{
202+
/*{
187203
/// Generic version of the scalar atom structs.
188204
#[repr(C, align(8))]
189205
struct Scalar<B: Sized> {
190206
atom: sys::LV2_Atom,
191207
body: B,
192208
}
193209
194-
let scalar: &Scalar<A::InternalType> =
210+
let scalar: &Scalar<A::WriteParameter> =
195211
unsafe { raw_space.read().next_value().unwrap() };
196212
197213
assert_eq!(scalar.atom.type_, urid);
@@ -206,17 +222,17 @@ mod tests {
206222
.read(urid, ())
207223
.unwrap();
208224
209-
assert_eq!(read_value, value);
210-
}
225+
assert_eq!(*read_value, value);
226+
}*/
211227
}
212228

213229
#[test]
214230
fn test_scalars() {
215-
test_scalar::<Double>(42.0);
216-
test_scalar::<Float>(42.0);
217-
test_scalar::<Long>(42);
218-
test_scalar::<Int>(42);
219-
test_scalar::<Bool>(1);
220-
test_scalar::<AtomURID>(URID::try_from(1).unwrap());
231+
test_scalar::<Double, _>(42.0);
232+
test_scalar::<Float, _>(42.0);
233+
test_scalar::<Long, _>(42);
234+
test_scalar::<Int, _>(42);
235+
test_scalar::<Bool, _>(1);
236+
test_scalar::<AtomURID, _>(URID::try_from(1).unwrap());
221237
}
222238
}

atom/src/atoms/sequence.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -360,11 +360,11 @@ mod tests {
360360

361361
let (stamp, atom) = reader.next().unwrap();
362362
assert_eq!(stamp, TimeStamp::Frames(0));
363-
assert_eq!(atom.read::<Int>(urids.atom.int, ()).unwrap(), 42);
363+
assert_eq!(*atom.read::<Int>(urids.atom.int, ()).unwrap(), 42);
364364

365365
let (stamp, atom) = reader.next().unwrap();
366366
assert_eq!(stamp, TimeStamp::Frames(1));
367-
assert_eq!(atom.read::<Long>(urids.atom.long, ()).unwrap(), 17);
367+
assert_eq!(*atom.read::<Long>(urids.atom.long, ()).unwrap(), 17);
368368

369369
assert!(reader.next().is_none());
370370
}

atom/src/atoms/vector.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ use std::mem::{size_of, MaybeUninit};
3838
/// An atom containg an array of scalar atom bodies.
3939
///
4040
/// [See also the module documentation.](index.html)
41-
pub struct Vector<C: ScalarAtom> {
41+
pub struct Vector<C> {
4242
child: PhantomData<C>,
4343
}
4444

45-
unsafe impl<C: ScalarAtom> UriBound for Vector<C> {
45+
unsafe impl<C> UriBound for Vector<C> {
4646
const URI: &'static [u8] = sys::LV2_ATOM__Vector;
4747
}
4848

49-
impl<'handle, 'space: 'handle, C: ScalarAtom> Atom<'handle, 'space> for Vector<C>
49+
impl<'handle, 'space: 'handle, C: ScalarAtom<'handle, 'space>> Atom<'handle, 'space> for Vector<C>
5050
where
5151
C: 'space,
5252
{
@@ -92,12 +92,12 @@ where
9292
/// Handle to append elements to a vector.
9393
///
9494
/// This works by allocating a slice of memory behind the vector and then writing your data to it.
95-
pub struct VectorWriter<'handle, 'space, A: ScalarAtom> {
95+
pub struct VectorWriter<'handle, 'space, A: ScalarAtom<'handle, 'space>> {
9696
frame: AtomSpaceWriter<'handle, 'space>,
9797
type_: PhantomData<A>,
9898
}
9999

100-
impl<'handle, 'space, A: ScalarAtom> VectorWriter<'handle, 'space, A> {
100+
impl<'handle, 'space, A: ScalarAtom<'handle, 'space>> VectorWriter<'handle, 'space, A> {
101101
/// Push a single value to the vector.
102102
#[inline]
103103
pub fn push(&mut self, child: A::InternalType) -> Option<&mut A::InternalType> {

atom/src/lib.rs

+15-7
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,16 @@ pub mod prelude {
9292
pub use crate::{atoms::AtomURIDCollection, Atom, UnidentifiedAtom};
9393
}
9494

95+
trait AtomHandle<'a> {
96+
type Handle: 'a;
97+
}
98+
9599
/// Atom type.
96100
///
97101
/// This is the foundation of this crate: Types that implement `Atom` define the reading and writing functions for an atom type. However, these types will never be constructed; They are only names to be used for generic type arguments.
98102
///
99103
/// This trait has two lifetime parameters: The first one is the lifetime of the atom in memory. In practice, this will often be `'static`, but it's good to keep it generic for testing purposes. The second parameter is the lifetime of the `MutSpace` borrowed by the `FramedMutSpace` parameter in the `write` method. Since the `WriteParameter` may contain this `FramedMutSpace`, it has to be assured that it lives long enough. Since the referenced `MutSpace` also has to borrow the atom, it may not live longer than the atom.
100-
pub trait Atom<'handle, 'space: 'handle>: UriBound {
104+
pub trait Atom: UriBound {
101105
/// The atom-specific parameter of the `read` function.
102106
///
103107
/// If your atom does not need a reading parameter, you may set it to `()`.
@@ -106,7 +110,7 @@ pub trait Atom<'handle, 'space: 'handle>: UriBound {
106110
/// The return value of the `read` function.
107111
///
108112
/// It may contain a reference to the atom and therefore may not outlive it.
109-
type ReadHandle: 'handle;
113+
type ReadHandle: for<'a> AtomHandle<'a>;
110114

111115
/// The atom-specific parameter of the `write` function.
112116
///
@@ -116,7 +120,7 @@ pub trait Atom<'handle, 'space: 'handle>: UriBound {
116120
/// The return value of the `write` function.
117121
///
118122
/// It may contain a reference to a `MutSpace` and therefore may not outlive it.
119-
type WriteHandle: 'handle;
123+
type WriteHandle: for<'a> AtomHandle<'a>;
120124

121125
/// Reads the body of the atom.
122126
///
@@ -128,10 +132,10 @@ pub trait Atom<'handle, 'space: 'handle>: UriBound {
128132
///
129133
/// The caller needs to ensure that the given [`Space`] contains a valid instance of this atom,
130134
/// or the resulting `ReadHandle` will be completely invalid, and Undefined Behavior will happen.
131-
unsafe fn read(
135+
unsafe fn read<'handle, 'space: 'handle>(
132136
body: &'space AtomSpace,
133137
parameter: Self::ReadParameter,
134-
) -> Option<Self::ReadHandle>;
138+
) -> Option<<Self::ReadHandle as AtomHandle<'handle>>::Handle>;
135139

136140
/// Initialize the body of the atom.
137141
///
@@ -141,10 +145,14 @@ pub trait Atom<'handle, 'space: 'handle>: UriBound {
141145
/// The frame of the atom was already initialized, containing the URID.
142146
///
143147
/// If space is insufficient, you may not panic and return `None` instead. The written results are assumed to be malformed.
144-
fn init(
148+
fn init<'handle, 'space: 'handle>(
145149
frame: AtomSpaceWriter<'handle, 'space>,
146150
parameter: Self::WriteParameter,
147-
) -> Option<Self::WriteHandle>;
151+
) -> Option<<Self::WriteHandle as AtomHandle<'handle>>::Handle>;
152+
}
153+
154+
pub trait BackAsSpace: Atom {
155+
fn back_as_space(value: Self::ReadHandle) -> &'handle AlignedSpace<u8>;
148156
}
149157

150158
/// An atom of yet unknown type.

atom/src/port.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ mod tests {
165165
.read(urids.chunk, ())
166166
.unwrap();
167167
let reader = unsafe { AtomPort::input_from_raw(NonNull::from(chunk).cast(), 0) };
168-
assert_eq!(reader.read::<Int>(urids.int, ()).unwrap(), 42);
168+
assert_eq!(*reader.read::<Int>(urids.int, ()).unwrap(), 42);
169169
}
170170
}
171171
}

atom/src/space/allocatable.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ pub trait SpaceAllocator<'space>: SpaceAllocatorImpl<'space> + Sized {
6868
}
6969

7070
#[inline]
71-
fn init_atom<'handle, A: Atom<'handle, 'space>>(
72-
&'handle mut self,
71+
fn init_atom<'handle, 'a: 'handle, A: Atom<'handle, 'space>>(
72+
&'a mut self,
7373
atom_type: URID<A>,
7474
write_parameter: A::WriteParameter,
7575
) -> Option<A::WriteHandle>

atom/src/space/cursor.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub struct SpaceCursor<'a> {
66
}
77

88
impl<'a> SpaceCursor<'a> {
9-
pub fn new(data: &'a mut [u8]) -> Self {
9+
pub fn new<'b: 'a>(data: &'b mut [u8]) -> Self {
1010
Self {
1111
data,
1212
allocated_length: 0,

buf-size/src/buffer_sizes.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ impl BufferSizes {
3333
options: &OptionsList,
3434
urids: &BufferSizesURIDCollection,
3535
) -> Self {
36-
BufferSizes {
36+
todo!()
37+
/*BufferSizes {
3738
min_block_length: options
3839
.read(urids.min_block_length, urids.atom_int, ())
3940
.map(|x| x.get()),
@@ -46,6 +47,6 @@ impl BufferSizes {
4647
sequence_size: options
4748
.read(urids.sequence_size, urids.atom_int, ())
4849
.map(|x| x.get()),
49-
}
50+
}*/
5051
}
5152
}

buf-size/src/options.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,19 @@ macro_rules! make_option {
1818
}
1919

2020
impl lv2_options::OptionType for $name {
21-
type AtomType = lv2_atom::scalar::Int;
21+
type AtomType = lv2_atom::atoms::scalar::Int;
2222

2323
#[inline]
2424
fn from_option_value<'a>(
25-
value: <lv2_atom::scalar::Int as Atom<'a, 'a>>::ReadHandle,
25+
value: <lv2_atom::atoms::scalar::Int as Atom<'a, 'a>>::ReadHandle,
2626
) -> Option<Self> {
2727
Some(Self((*value)))
2828
}
2929

3030
#[inline]
31-
fn as_option_value<'a>(&'a self) -> <lv2_atom::scalar::Int as Atom<'a, 'a>>::ReadHandle {
31+
fn as_option_value<'a>(
32+
&'a self,
33+
) -> <lv2_atom::atoms::scalar::Int as Atom<'a, 'a>>::ReadHandle {
3234
&self.0
3335
}
3436
}

0 commit comments

Comments
 (0)