Skip to content

Commit 866ed97

Browse files
committed
Fix reusable connection message
1 parent bc76230 commit 866ed97

File tree

3 files changed

+12
-14
lines changed

3 files changed

+12
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/)
1515
### Fixed
1616

1717
- Documentation example fixes
18+
- Fix the polled message being unsafely clonable
1819

1920
## [0.2.0] - 2024-03-25
2021

src/connection.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{ffi::CString, mem};
1+
use std::{ffi::CString, marker::PhantomData, mem};
22

33
use ::leap_sys::*;
44

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

1814
impl Drop for Connection {
@@ -45,7 +41,6 @@ impl Connection {
4541

4642
Ok(Self {
4743
handle: leap_connection,
48-
connection_message: None,
4944
})
5045
}
5146

@@ -97,17 +92,13 @@ impl Connection {
9792
#[doc = " times out, this method will return eLeapRS_Timeout. The evt pointer will reference a"]
9893
#[doc = " message of type eLeapEventType_None."]
9994
#[doc = " @since 3.0.0"]
100-
pub fn poll(&mut self, timeout: u32) -> Result<&ConnectionMessage, Error> {
101-
// The code after will invalidate it.
102-
self.connection_message = None;
95+
pub fn poll(&mut self, timeout: u32) -> Result<ConnectionMessage, Error> {
10396
let mut msg: LEAP_CONNECTION_MESSAGE;
10497
unsafe {
10598
msg = mem::zeroed();
10699
leap_try(LeapPollConnection(self.handle, timeout, &mut msg))?;
107100
}
108-
self.connection_message = Some(ConnectionMessage(msg));
109-
110-
Ok(self.connection_message.as_ref().unwrap())
101+
Ok(ConnectionMessage(msg, PhantomData))
111102
}
112103

113104
#[doc = " Retrieves a list of Ultraleap Tracking camera devices currently attached to the system."]

src/connection_message.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::marker::PhantomData;
2+
13
use derive_deref::Deref;
24
use leap_sys::LEAP_CONNECTION_MESSAGE;
35

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

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

0 commit comments

Comments
 (0)