From 2ca97fdc6b8cb76694629acbc4fb20f08d1b390a Mon Sep 17 00:00:00 2001 From: Philipp Oppermann Date: Tue, 21 Mar 2023 12:59:48 +0100 Subject: [PATCH] Remove lifetime from `Event` type Don't bind the lifetime of the event to the `next` call anymore. This makes it possible to use the original event in Python, which does not support borrowed data or lifetimes. The drawback is that we no longer have the guarantee that an event is freed before the next call to `recv`. --- apis/c++/node/src/lib.rs | 6 +++--- apis/python/node/src/lib.rs | 4 ++-- apis/rust/node/src/event.rs | 27 +++++++++++---------------- 3 files changed, 16 insertions(+), 21 deletions(-) diff --git a/apis/c++/node/src/lib.rs b/apis/c++/node/src/lib.rs index 2e5f1314..e953961a 100644 --- a/apis/c++/node/src/lib.rs +++ b/apis/c++/node/src/lib.rs @@ -30,11 +30,11 @@ mod ffi { extern "Rust" { type Events; type OutputSender; - type DoraEvent<'a>; + type DoraEvent; fn init_dora_node() -> Result; - fn next_event(inputs: &mut Box) -> Box>; + fn next_event(inputs: &mut Box) -> Box; fn event_type(event: &Box) -> DoraEventType; fn event_as_input(event: Box) -> Result; fn send_output( @@ -62,7 +62,7 @@ fn next_event(events: &mut Box) -> Box { Box::new(DoraEvent(events.0.recv())) } -pub struct DoraEvent<'a>(Option>); +pub struct DoraEvent(Option); fn event_type(event: &DoraEvent) -> ffi::DoraEventType { match &event.0 { diff --git a/apis/python/node/src/lib.rs b/apis/python/node/src/lib.rs index eb9eb1fd..113ced22 100644 --- a/apis/python/node/src/lib.rs +++ b/apis/python/node/src/lib.rs @@ -14,9 +14,9 @@ pub struct Node { node: DoraNode, } -pub struct PyInput<'a>(Event<'a>); +pub struct PyInput(Event); -impl IntoPy for PyInput<'_> { +impl IntoPy for PyInput { fn into_py(self, py: Python) -> PyObject { let dict = PyDict::new(py); diff --git a/apis/rust/node/src/event.rs b/apis/rust/node/src/event.rs index 090513ef..5cd96571 100644 --- a/apis/rust/node/src/event.rs +++ b/apis/rust/node/src/event.rs @@ -9,7 +9,7 @@ use shared_memory::{Shmem, ShmemConf}; #[derive(Debug)] #[non_exhaustive] -pub enum Event<'a> { +pub enum Event { Stop, Reload { operator_id: Option, @@ -17,7 +17,7 @@ pub enum Event<'a> { Input { id: DataId, metadata: Metadata<'static>, - data: Option>, + data: Option, }, InputClosed { id: DataId, @@ -25,15 +25,15 @@ pub enum Event<'a> { Error(String), } -pub enum Data<'a> { +pub enum Data { Vec(Vec), SharedMemory { - data: MappedInputData<'a>, - _drop: std::sync::mpsc::Sender<()>, + data: MappedInputData, + _drop: flume::Sender<()>, }, } -impl std::ops::Deref for Data<'_> { +impl std::ops::Deref for Data { type Target = [u8]; fn deref(&self) -> &Self::Target { @@ -44,33 +44,28 @@ impl std::ops::Deref for Data<'_> { } } -impl std::fmt::Debug for Data<'_> { +impl std::fmt::Debug for Data { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("Data").finish_non_exhaustive() } } -pub struct MappedInputData<'a> { +pub struct MappedInputData { memory: Shmem, len: usize, - _data: PhantomData<&'a [u8]>, } -impl MappedInputData<'_> { +impl MappedInputData { pub(crate) unsafe fn map(shared_memory_id: &str, len: usize) -> eyre::Result { let memory = ShmemConf::new() .os_id(shared_memory_id) .open() .wrap_err("failed to map shared memory input")?; - Ok(MappedInputData { - memory, - len, - _data: PhantomData, - }) + Ok(MappedInputData { memory, len }) } } -impl std::ops::Deref for MappedInputData<'_> { +impl std::ops::Deref for MappedInputData { type Target = [u8]; fn deref(&self) -> &Self::Target {