diff --git a/atom/src/port.rs b/atom/src/port.rs index 155c4df4..ab70cd10 100644 --- a/atom/src/port.rs +++ b/atom/src/port.rs @@ -110,15 +110,23 @@ impl PortType for AtomPort { type OutputPortType = PortWriter<'static>; #[inline] - unsafe fn input_from_raw(pointer: NonNull, _sample_count: u32) -> PortReader<'static> { - let header = AtomHeader::from_raw(pointer.cast().as_ref()); - PortReader::new(UnidentifiedAtom::from_header(header)) + unsafe fn input_from_raw( + pointer: NonNull, + _sample_count: u32, + ) -> *const PortReader<'static> { + todo!(); + //let header = AtomHeader::from_raw(pointer.cast().as_ref()); + //PortReader::new(UnidentifiedAtom::from_header(header)) } #[inline] - unsafe fn output_from_raw(pointer: NonNull, _sample_count: u32) -> PortWriter<'static> { - let header = AtomHeader::from_raw_mut(pointer.cast().as_mut()); - PortWriter::new(UnidentifiedAtom::from_header_mut(header).body_mut()) + unsafe fn output_from_raw( + pointer: NonNull, + _sample_count: u32, + ) -> *mut PortWriter<'static> { + todo!(); + //let header = AtomHeader::from_raw_mut(pointer.cast().as_mut()); + //PortWriter::new(UnidentifiedAtom::from_header_mut(header).body_mut()) } } diff --git a/core/src/feature/mod.rs b/core/src/feature/mod.rs index 3871d1eb..d001e55d 100644 --- a/core/src/feature/mod.rs +++ b/core/src/feature/mod.rs @@ -9,6 +9,9 @@ pub use cache::FeatureCache; pub use core_features::*; pub use descriptor::FeatureDescriptor; +#[cfg(feature = "lv2-core-derive")] +pub use lv2_core_derive::FeatureCollection; + use std::ffi::c_void; /// All threading contexts of LV2 interface methods. diff --git a/core/src/port.rs b/core/src/port.rs index 0c7c16e6..c844b968 100644 --- a/core/src/port.rs +++ b/core/src/port.rs @@ -13,17 +13,14 @@ use std::ffi::c_void; use std::ops::{Deref, DerefMut}; use std::ptr::NonNull; -#[cfg(feature = "lv2-core-derive")] -pub use lv2_core_derive::*; - /// Generalization of port types. /// /// A port can read input or create a pointer to the output, but the exact type of input/output (pointer) depends on the type of port. This trait generalizes these types and behaviour. pub trait PortType { /// The type of input read by the port. - type InputPortType: Sized; + type InputPortType: ?Sized; /// The type of output reference created by the port. - type OutputPortType: Sized; + type OutputPortType: ?Sized; /// Read data from the pointer or create a reference to the input. /// @@ -32,7 +29,10 @@ pub trait PortType { /// # Safety /// /// This method is unsafe because one needs to de-reference a raw pointer to implement this method. - unsafe fn input_from_raw(pointer: NonNull, sample_count: u32) -> Self::InputPortType; + unsafe fn input_from_raw( + pointer: NonNull, + sample_count: u32, + ) -> *const Self::InputPortType; /// Create a reference to the data where output should be written to. /// @@ -41,7 +41,10 @@ pub trait PortType { /// # Safety /// /// This method is unsafe because one needs to de-reference a raw pointer to implement this method. - unsafe fn output_from_raw(pointer: NonNull, sample_count: u32) -> Self::OutputPortType; + unsafe fn output_from_raw( + pointer: NonNull, + sample_count: u32, + ) -> *mut Self::OutputPortType; } /// Abstraction of safe port handles. @@ -60,7 +63,7 @@ pub trait PortHandle: Sized { /// /// Fields of this type can be dereferenced to the input type of the port type. pub struct InputPort { - port: T::InputPortType, + port: *const T::InputPortType, } impl Deref for InputPort { @@ -68,11 +71,14 @@ impl Deref for InputPort { #[inline] fn deref(&self) -> &Self::Target { - &self.port + unsafe { &*self.port } } } -impl PortHandle for InputPort { +impl PortHandle for InputPort +where + T: PortType, +{ #[inline] unsafe fn from_raw(pointer: *mut c_void, sample_count: u32) -> Option { Some(Self { @@ -85,7 +91,7 @@ impl PortHandle for InputPort { /// /// Fields of this type can be dereferenced to the output type of the port type. pub struct OutputPort { - port: T::OutputPortType, + port: *mut T::OutputPortType, } impl Deref for OutputPort { @@ -93,14 +99,14 @@ impl Deref for OutputPort { #[inline] fn deref(&self) -> &Self::Target { - &self.port + unsafe { &*self.port } } } impl DerefMut for OutputPort { #[inline] fn deref_mut(&mut self) -> &mut Self::Target { - &mut self.port + unsafe { &mut *self.port } } } @@ -126,7 +132,8 @@ impl PortHandle for Option { /// # Implementing /// /// The most convenient way to create a port collections is to define a struct with port types from the [`port`](index.html) module and then simply derive `PortCollection` for it. An example: -/// +/// ``` +/// # pub use lv2_core_derive::*; /// use lv2_core::port::*; /// /// #[derive(PortCollection)] @@ -137,6 +144,7 @@ impl PortHandle for Option { /// control_output: OutputPort, /// optional_control_input: Option>, /// } +/// ``` /// /// Please note that port indices are mapped in the order of occurrence; In our example, the implementation will treat `audio_input` as port `0`, `audio_output` as port `1` and so on. Therefore, your plugin definition and your port collection have to match. Otherwise, undefined behaviour will occur. pub trait PortCollection: Sized { diff --git a/core/src/port/audio.rs b/core/src/port/audio.rs index ac755e90..2f3c51c1 100644 --- a/core/src/port/audio.rs +++ b/core/src/port/audio.rs @@ -66,16 +66,22 @@ unsafe impl UriBound for Audio { } impl PortType for Audio { - type InputPortType = &'static [f32]; - type OutputPortType = &'static mut [f32]; + type InputPortType = [f32]; + type OutputPortType = [f32]; #[inline] - unsafe fn input_from_raw(pointer: NonNull, sample_count: u32) -> Self::InputPortType { + unsafe fn input_from_raw( + pointer: NonNull, + sample_count: u32, + ) -> *const Self::InputPortType { std::slice::from_raw_parts(pointer.as_ptr() as *const f32, sample_count as usize) } #[inline] - unsafe fn output_from_raw(pointer: NonNull, sample_count: u32) -> Self::OutputPortType { + unsafe fn output_from_raw( + pointer: NonNull, + sample_count: u32, + ) -> *mut Self::OutputPortType { std::slice::from_raw_parts_mut(pointer.as_ptr() as *mut f32, sample_count as usize) } } @@ -132,11 +138,15 @@ unsafe impl UriBound for InPlaceAudio { } impl PortType for InPlaceAudio { - type InputPortType = &'static [Cell]; - type OutputPortType = &'static [Cell]; + type InputPortType = [Cell]; + type OutputPortType = [Cell]; #[inline] - unsafe fn input_from_raw(pointer: NonNull, sample_count: u32) -> Self::InputPortType { + unsafe fn input_from_raw( + pointer: NonNull, + sample_count: u32, + ) -> *const Self::InputPortType { + //std::slice::from_raw_parts(pointer.as_ptr() as *const Cell, sample_count as usize) Cell::from_mut(std::slice::from_raw_parts_mut( pointer.as_ptr() as *mut f32, sample_count as usize, @@ -145,11 +155,15 @@ impl PortType for InPlaceAudio { } #[inline] - unsafe fn output_from_raw(pointer: NonNull, sample_count: u32) -> Self::OutputPortType { - Cell::from_mut(std::slice::from_raw_parts_mut( - pointer.as_ptr() as *mut f32, - sample_count as usize, - )) - .as_slice_of_cells() + unsafe fn output_from_raw( + pointer: NonNull, + sample_count: u32, + ) -> *mut Self::OutputPortType { + std::slice::from_raw_parts_mut(pointer.as_ptr() as *mut Cell, sample_count as usize) + //Cell::from_mut(std::slice::from_raw_parts_mut( + // pointer.as_ptr() as *mut f32, + // sample_count as usize, + //)) + //.as_slice_of_cells() } } diff --git a/core/src/port/control.rs b/core/src/port/control.rs index 4f9c953f..3cc378dc 100644 --- a/core/src/port/control.rs +++ b/core/src/port/control.rs @@ -66,16 +66,16 @@ unsafe impl UriBound for Control { impl PortType for Control { type InputPortType = f32; - type OutputPortType = &'static mut f32; + type OutputPortType = f32; #[inline] - unsafe fn input_from_raw(pointer: NonNull, _sample_count: u32) -> f32 { - *(pointer.cast().as_ref()) + unsafe fn input_from_raw(pointer: NonNull, _sample_count: u32) -> *const f32 { + pointer.as_ptr() as *const f32 } #[inline] - unsafe fn output_from_raw(pointer: NonNull, _sample_count: u32) -> &'static mut f32 { - (pointer.as_ptr() as *mut f32).as_mut().unwrap() + unsafe fn output_from_raw(pointer: NonNull, _sample_count: u32) -> *mut f32 { + pointer.as_ptr() as *mut f32 } } @@ -130,19 +130,22 @@ unsafe impl UriBound for InPlaceControl { } impl PortType for InPlaceControl { - type InputPortType = &'static Cell; - type OutputPortType = &'static Cell; + type InputPortType = Cell; + type OutputPortType = Cell; #[inline] - unsafe fn input_from_raw(pointer: NonNull, _sample_count: u32) -> Self::InputPortType { - Cell::from_mut(&mut *(pointer.as_ptr() as *mut f32)) + unsafe fn input_from_raw( + pointer: NonNull, + _sample_count: u32, + ) -> *const Self::InputPortType { + pointer.as_ptr() as _ } #[inline] unsafe fn output_from_raw( pointer: NonNull, _sample_count: u32, - ) -> Self::OutputPortType { - Cell::from_mut(&mut *(pointer.as_ptr() as *mut f32)) + ) -> *mut Self::OutputPortType { + pointer.as_ptr() as _ } } diff --git a/core/src/port/cv.rs b/core/src/port/cv.rs index 9455aa0c..592f3f26 100644 --- a/core/src/port/cv.rs +++ b/core/src/port/cv.rs @@ -79,16 +79,22 @@ unsafe impl UriBound for CV { } impl PortType for CV { - type InputPortType = &'static [f32]; - type OutputPortType = &'static mut [f32]; + type InputPortType = [f32]; + type OutputPortType = [f32]; #[inline] - unsafe fn input_from_raw(pointer: NonNull, sample_count: u32) -> Self::InputPortType { + unsafe fn input_from_raw( + pointer: NonNull, + sample_count: u32, + ) -> *const Self::InputPortType { std::slice::from_raw_parts(pointer.as_ptr() as *const f32, sample_count as usize) } #[inline] - unsafe fn output_from_raw(pointer: NonNull, sample_count: u32) -> Self::OutputPortType { + unsafe fn output_from_raw( + pointer: NonNull, + sample_count: u32, + ) -> *mut Self::OutputPortType { std::slice::from_raw_parts_mut(pointer.as_ptr() as *mut f32, sample_count as usize) } } @@ -158,24 +164,22 @@ unsafe impl UriBound for InPlaceCV { } impl PortType for InPlaceCV { - type InputPortType = &'static [Cell]; - type OutputPortType = &'static [Cell]; + type InputPortType = [Cell]; + type OutputPortType = [Cell]; #[inline] - unsafe fn input_from_raw(pointer: NonNull, sample_count: u32) -> Self::InputPortType { - Cell::from_mut(std::slice::from_raw_parts_mut( - pointer.as_ptr() as *mut f32, - sample_count as usize, - )) - .as_slice_of_cells() + unsafe fn input_from_raw( + pointer: NonNull, + sample_count: u32, + ) -> *const Self::InputPortType { + std::slice::from_raw_parts(pointer.as_ptr() as *const Cell, sample_count as usize) } #[inline] - unsafe fn output_from_raw(pointer: NonNull, sample_count: u32) -> Self::OutputPortType { - Cell::from_mut(std::slice::from_raw_parts_mut( - pointer.as_ptr() as *mut f32, - sample_count as usize, - )) - .as_slice_of_cells() + unsafe fn output_from_raw( + pointer: NonNull, + sample_count: u32, + ) -> *mut Self::OutputPortType { + std::slice::from_raw_parts_mut(pointer.as_ptr() as *mut Cell, sample_count as usize) } }