Browse Source

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`.
tags/v0.2.3-rc
Philipp Oppermann 2 years ago
parent
commit
2ca97fdc6b
Failed to extract signature
3 changed files with 16 additions and 21 deletions
  1. +3
    -3
      apis/c++/node/src/lib.rs
  2. +2
    -2
      apis/python/node/src/lib.rs
  3. +11
    -16
      apis/rust/node/src/event.rs

+ 3
- 3
apis/c++/node/src/lib.rs View File

@@ -30,11 +30,11 @@ mod ffi {
extern "Rust" {
type Events;
type OutputSender;
type DoraEvent<'a>;
type DoraEvent;

fn init_dora_node() -> Result<DoraNode>;

fn next_event(inputs: &mut Box<Events>) -> Box<DoraEvent<'_>>;
fn next_event(inputs: &mut Box<Events>) -> Box<DoraEvent>;
fn event_type(event: &Box<DoraEvent>) -> DoraEventType;
fn event_as_input(event: Box<DoraEvent>) -> Result<DoraInput>;
fn send_output(
@@ -62,7 +62,7 @@ fn next_event(events: &mut Box<Events>) -> Box<DoraEvent> {
Box::new(DoraEvent(events.0.recv()))
}

pub struct DoraEvent<'a>(Option<Event<'a>>);
pub struct DoraEvent(Option<Event>);

fn event_type(event: &DoraEvent) -> ffi::DoraEventType {
match &event.0 {


+ 2
- 2
apis/python/node/src/lib.rs View File

@@ -14,9 +14,9 @@ pub struct Node {
node: DoraNode,
}

pub struct PyInput<'a>(Event<'a>);
pub struct PyInput(Event);

impl IntoPy<PyObject> for PyInput<'_> {
impl IntoPy<PyObject> for PyInput {
fn into_py(self, py: Python) -> PyObject {
let dict = PyDict::new(py);



+ 11
- 16
apis/rust/node/src/event.rs View File

@@ -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<OperatorId>,
@@ -17,7 +17,7 @@ pub enum Event<'a> {
Input {
id: DataId,
metadata: Metadata<'static>,
data: Option<Data<'a>>,
data: Option<Data>,
},
InputClosed {
id: DataId,
@@ -25,15 +25,15 @@ pub enum Event<'a> {
Error(String),
}

pub enum Data<'a> {
pub enum Data {
Vec(Vec<u8>),
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<Self> {
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 {


Loading…
Cancel
Save