Skip to content

Commit

Permalink
Fix reusable connection message
Browse files Browse the repository at this point in the history
  • Loading branch information
plule committed Mar 25, 2024
1 parent bc76230 commit 866ed97
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/)
### Fixed

- Documentation example fixes
- Fix the polled message being unsafely clonable

## [0.2.0] - 2024-03-25

Expand Down
15 changes: 3 additions & 12 deletions src/connection.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{ffi::CString, mem};
use std::{ffi::CString, marker::PhantomData, mem};

use ::leap_sys::*;

Expand All @@ -9,10 +9,6 @@ use crate::*;
#[doc = " @since 3.0.0"]
pub struct Connection {
handle: LEAP_CONNECTION,
// Each call to call() invalidates the connection message pointer,
// and it is distroy on the connection drop.
// Only distribute non mutable references of this one.
connection_message: Option<ConnectionMessage>,
}

impl Drop for Connection {
Expand Down Expand Up @@ -45,7 +41,6 @@ impl Connection {

Ok(Self {
handle: leap_connection,
connection_message: None,
})
}

Expand Down Expand Up @@ -97,17 +92,13 @@ impl Connection {
#[doc = " times out, this method will return eLeapRS_Timeout. The evt pointer will reference a"]
#[doc = " message of type eLeapEventType_None."]
#[doc = " @since 3.0.0"]
pub fn poll(&mut self, timeout: u32) -> Result<&ConnectionMessage, Error> {
// The code after will invalidate it.
self.connection_message = None;
pub fn poll(&mut self, timeout: u32) -> Result<ConnectionMessage, Error> {
let mut msg: LEAP_CONNECTION_MESSAGE;
unsafe {
msg = mem::zeroed();
leap_try(LeapPollConnection(self.handle, timeout, &mut msg))?;
}
self.connection_message = Some(ConnectionMessage(msg));

Ok(self.connection_message.as_ref().unwrap())
Ok(ConnectionMessage(msg, PhantomData))
}

#[doc = " Retrieves a list of Ultraleap Tracking camera devices currently attached to the system."]
Expand Down
10 changes: 8 additions & 2 deletions src/connection_message.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::marker::PhantomData;

use derive_deref::Deref;
use leap_sys::LEAP_CONNECTION_MESSAGE;

Expand All @@ -7,9 +9,13 @@ use crate::event::EventRef;
#[doc = " Set by calling LeapPollConnection()."]
#[doc = " @since 3.0.0"]
#[derive(Deref, Clone, Copy)]
pub struct ConnectionMessage(pub(crate) LEAP_CONNECTION_MESSAGE);
pub struct ConnectionMessage<'a>(
pub(crate) LEAP_CONNECTION_MESSAGE,
/// Holds a lifetime to invalidate previous messages
pub(crate) PhantomData<&'a ()>,
);

impl ConnectionMessage {
impl ConnectionMessage<'_> {
#[doc = " A pointer to the event data for the current type of message. @since 3.0.0"]
pub fn event(&self) -> EventRef {
(self.type_, &self.__bindgen_anon_1).into()
Expand Down

0 comments on commit 866ed97

Please sign in to comment.