Skip to content

Commit

Permalink
Merge pull request #69 from plule/ref_naming
Browse files Browse the repository at this point in the history
Add the -Ref suffix to the reference structure, implement Copy
  • Loading branch information
plule authored Mar 24, 2024
2 parents 79911b9 + 56c796e commit 905960a
Show file tree
Hide file tree
Showing 41 changed files with 296 additions and 289 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/)
### Added

- All the wrapper structures now implement `Deref` of the wrapped `leap-sys` structures
- All the reference wrapper structures now implement `Clone` and `Copy`
- The opt-in features `glam` and `nalgebra` add conversion method for vectors and quaternions to these libraries.

### Changed

- Improved the examples to avoid common timeout unwrap()
- The vectors and quaternion are not anymore copied
- **Breaking** All the non-owning wrappers (most of them) are now named with `Ref` as a suffix.
`Hand` becomes `HandRef`, `Digit` becomes `DigitRef`, etc...

### Removed

- **Breaking** All the trivial accessor functions were removed, in favour of the `Deref` trait.
- **Breaking** All the trivial accessor functions were removed, in favour of the `Deref` trait

### Fixed

Expand Down
2 changes: 1 addition & 1 deletion examples/count_hands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ fn main() {
loop {
if let Ok(msg) = c.poll(1000) {
match msg.event() {
Event::Tracking(e) => println!("{} hand(s)", e.hands().len()),
EventRef::Tracking(e) => println!("{} hand(s)", e.hands().len()),
_ => {}
}
}
Expand Down
18 changes: 9 additions & 9 deletions examples/interactive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ fn main() {
connection.open().expect("Failed to open the connection");

connection.wait_for("Connecting to the service...".to_string(), |e| match e {
Event::Connection(e) => {
EventRef::Connection(e) => {
let flags = e.flags();
Msg::Success(format!("Connected. Service state: {:?}", flags))
}
_ => Msg::None,
});

connection.wait_for("Waiting for a device...".to_string(), |e| match e {
Event::Device(e) => {
EventRef::Device(e) => {
let device_info = e
.device()
.open()
Expand Down Expand Up @@ -46,7 +46,7 @@ fn main() {
connection.wait_for(
"Waiting for the tracking mode message...".to_string(),
|e| match e {
Event::TrackingMode(e) => {
EventRef::TrackingMode(e) => {
Msg::Success(format!("Tracking mode: {:#?}", e.current_tracking_mode()))
}
_ => Msg::None,
Expand All @@ -55,7 +55,7 @@ fn main() {
}

connection.wait_for("Waiting for a hand...".to_string(), |e| match e {
Event::Tracking(e) => {
EventRef::Tracking(e) => {
if !e.hands().is_empty() {
Msg::Success("Got a hand".to_string())
} else {
Expand All @@ -66,7 +66,7 @@ fn main() {
});

connection.wait_for("Close the hand".to_string(), |e| match e {
Event::Tracking(e) => {
EventRef::Tracking(e) => {
if let Some(hand) = e.hands().first() {
let grab_strength = hand.grab_strength;
if grab_strength >= 1.0 {
Expand All @@ -82,7 +82,7 @@ fn main() {
});

connection.wait_for("Open the hand".to_string(), |e| match e {
Event::Tracking(e) => {
EventRef::Tracking(e) => {
if let Some(hand) = e.hands().first() {
let ungrab_strength = 1.0 - hand.grab_strength;
if ungrab_strength >= 0.999 {
Expand All @@ -102,7 +102,7 @@ fn main() {
.expect("Failed to set policy flags");

connection.wait_for("Reading image".to_string(), |e| match e {
Event::Image(e) => {
EventRef::Image(e) => {
let w = e.images()[0].properties().width;
let h = e.images()[0].properties().height;
let images = e.images();
Expand Down Expand Up @@ -130,13 +130,13 @@ pub enum Msg {
trait WaitFor {
fn wait_for<F>(&mut self, message: String, condition: F)
where
F: Fn(&Event) -> Msg;
F: Fn(&EventRef) -> Msg;
}

impl WaitFor for Connection {
fn wait_for<F>(&mut self, message: String, condition: F)
where
F: Fn(&Event) -> Msg,
F: Fn(&EventRef) -> Msg,
{
let mut throbber = Throbber::new().interval(Duration::from_millis(100));

Expand Down
46 changes: 23 additions & 23 deletions examples/watch_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,30 @@ fn main() {
loop {
if let Ok(message) = connection.poll(1000) {
match message.event() {
Event::None => println!("None"),
Event::Connection(_) => println!("Connection"),
Event::ConnectionLost(_) => println!("ConnectionLost"),
Event::Device(_) => println!("Device"),
Event::DeviceFailure(_) => println!("DeviceFailure"),
Event::Policy(_) => println!("Policy"),
Event::Tracking(_) => {} // spam
Event::ImageRequestError => println!("ImageRequestError"),
Event::ImageComplete => println!("ImageComplete"),
Event::LogEvent(_) => println!("Log"),
Event::DeviceLost => println!("DeviceLost"),
Event::ConfigResponse(_) => println!("ConfigResponse"),
Event::ConfigChange(_) => println!("ConfigChange"),
Event::DeviceStatusChange(_) => println!("DeviceStatusChange"),
Event::DroppedFrame(_) => println!("DroppedFrame"),
Event::Image(_) => println!("Image"),
Event::PointMappingChange(_) => println!("PointMappingChange"),
EventRef::None => println!("None"),
EventRef::Connection(_) => println!("Connection"),
EventRef::ConnectionLost(_) => println!("ConnectionLost"),
EventRef::Device(_) => println!("Device"),
EventRef::DeviceFailure(_) => println!("DeviceFailure"),
EventRef::Policy(_) => println!("Policy"),
EventRef::Tracking(_) => {} // spam
EventRef::ImageRequestError => println!("ImageRequestError"),
EventRef::ImageComplete => println!("ImageComplete"),
EventRef::LogEvent(_) => println!("Log"),
EventRef::DeviceLost => println!("DeviceLost"),
EventRef::ConfigResponse(_) => println!("ConfigResponse"),
EventRef::ConfigChange(_) => println!("ConfigChange"),
EventRef::DeviceStatusChange(_) => println!("DeviceStatusChange"),
EventRef::DroppedFrame(_) => println!("DroppedFrame"),
EventRef::Image(_) => println!("Image"),
EventRef::PointMappingChange(_) => println!("PointMappingChange"),
#[cfg(feature = "gemini")]
Event::TrackingMode(_) => println!("TrackingMode"),
Event::LogEvents(_) => println!("LogEvents"),
Event::HeadPose(_) => println!("HeadPose"),
Event::Eyes(_) => println!("Eyes"),
Event::IMU(_) => println!("IMU"),
Event::Unknown(_) => println!("Unknown"),
EventRef::TrackingMode(_) => println!("TrackingMode"),
EventRef::LogEvents(_) => println!("LogEvents"),
EventRef::HeadPose(_) => println!("HeadPose"),
EventRef::Eyes(_) => println!("Eyes"),
EventRef::IMU(_) => println!("IMU"),
EventRef::Unknown(_) => println!("Unknown"),
}
}
}
Expand Down
20 changes: 10 additions & 10 deletions src/bone.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
use derive_deref::Deref;
use leap_sys::LEAP_BONE;

use crate::{LeapVector, Quaternion};
use crate::{LeapVectorRef, QuaternionRef};

#[doc = " Describes a bone's position and orientation."]
#[doc = ""]
#[doc = " Bones are members of the LEAP_DIGIT struct."]
#[doc = " @since 3.0.0"]
#[derive(Deref)]
pub struct Bone<'a>(pub(crate) &'a LEAP_BONE);
#[derive(Deref, Clone, Copy)]
pub struct BoneRef<'a>(pub(crate) &'a LEAP_BONE);

impl<'a> Bone<'a> {
impl<'a> BoneRef<'a> {
#[doc = " The base of the bone, closer to the heart. The bones origin. @since 3.0.0"]
pub fn prev_joint(&self) -> LeapVector {
LeapVector::from(&self.prev_joint)
pub fn prev_joint(&self) -> LeapVectorRef {
LeapVectorRef::from(&self.prev_joint)
}

#[doc = " The end of the bone, further from the heart. @since 3.0.0"]
pub fn next_joint(&self) -> LeapVector {
LeapVector::from(&self.next_joint)
pub fn next_joint(&self) -> LeapVectorRef {
LeapVectorRef::from(&self.next_joint)
}

#[doc = " Rotation in world space from the forward direction."]
#[doc = " Convert the quaternion to a matrix to derive the basis vectors."]
#[doc = " @since 3.1.2"]
pub fn rotation(&self) -> Quaternion {
Quaternion::from(&self.rotation)
pub fn rotation(&self) -> QuaternionRef {
QuaternionRef::from(&self.rotation)
}
}
10 changes: 5 additions & 5 deletions src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,7 @@ mod tests {

connection
.wait_for(|e| match e {
Event::Policy(_) => Some(()),
EventRef::Policy(_) => Some(()),
_ => None,
})
.expect("Did not receive policy change");
Expand Down Expand Up @@ -916,7 +916,7 @@ mod tests {
// Note: If events are not polled, the frame interpolation fails with "is not seer"
connection
.wait_for(|e| match e {
Event::Tracking(_) => Some(()),
EventRef::Tracking(_) => Some(()),
_ => None,
})
.expect("no tracking data");
Expand Down Expand Up @@ -1003,7 +1003,7 @@ mod tests {
.expect("Failed to request the config value");
connection
.wait_for(|e| match e {
Event::ConfigResponse(c) => {
EventRef::ConfigResponse(c) => {
if c.requestID != request_id {
None
} else if let Variant::Boolean(robust_mode_enabled) = c.value() {
Expand All @@ -1028,7 +1028,7 @@ mod tests {
// Note: If events are not polled, the frame interpolation fails with "is not seer"
connection
.wait_for(|e| match e {
Event::Tracking(_) => Some(()),
EventRef::Tracking(_) => Some(()),
_ => None,
})
.expect("no tracking data");
Expand Down Expand Up @@ -1066,7 +1066,7 @@ mod tests {

connection
.wait_for(|e| match e {
Event::Policy(_) => Some(()),
EventRef::Policy(_) => Some(()),
_ => None,
})
.expect("Did not receive policy change");
Expand Down
2 changes: 1 addition & 1 deletion src/connection_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::ConnectionStatus;
#[doc = " call LeapOpenConnection() to establish the connection; then call"]
#[doc = " LeapGetConnectionInfo(), which creates this struct, to check the connection status."]
#[doc = " @since 3.0.0"]
#[derive(Deref)]
#[derive(Deref, Clone, Copy)]
pub struct ConnectionInfo(pub(crate) LEAP_CONNECTION_INFO);

impl ConnectionInfo {
Expand Down
6 changes: 3 additions & 3 deletions src/connection_message.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use derive_deref::Deref;
use leap_sys::LEAP_CONNECTION_MESSAGE;

use crate::event::Event;
use crate::event::EventRef;

#[doc = " Defines a basic message from the LeapC message queue."]
#[doc = " Set by calling LeapPollConnection()."]
#[doc = " @since 3.0.0"]
#[derive(Deref)]
#[derive(Deref, Clone, Copy)]
pub struct ConnectionMessage(pub(crate) LEAP_CONNECTION_MESSAGE);

impl ConnectionMessage {
#[doc = " A pointer to the event data for the current type of message. @since 3.0.0"]
pub fn event(&self) -> Event {
pub fn event(&self) -> EventRef {
(self.type_, &self.__bindgen_anon_1).into()
}
}
2 changes: 1 addition & 1 deletion src/device_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::Device;
#[doc = " Get a LEAP_DEVICE_REF by calling LeapGetDeviceList(). Access a device by"]
#[doc = " calling LeapOpenDevice() with this reference. LeapOpenDevice() provides a"]
#[doc = " LEAP_DEVICE struct, which is a handle to an open device."]
#[derive(Deref)]
#[derive(Deref, Clone, Copy)]
pub struct DeviceRef(pub(crate) LEAP_DEVICE_REF);

impl DeviceRef {
Expand Down
34 changes: 17 additions & 17 deletions src/digit.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
use derive_deref::Deref;
use leap_sys::LEAP_DIGIT;

use crate::Bone;
use crate::BoneRef;

#[doc = " Describes the digit of a hand."]
#[doc = " Digits are members of the LEAP_HAND struct."]
#[doc = " @since 3.0.0"]
#[derive(Deref)]
pub struct Digit<'a>(pub(crate) &'a LEAP_DIGIT);
#[derive(Deref, Clone, Copy)]
pub struct DigitRef<'a>(pub(crate) &'a LEAP_DIGIT);

impl<'a> Digit<'a> {
impl<'a> DigitRef<'a> {
#[doc = " All the bones of a digit as an iterable collection. @since 3.0.0"]
pub fn bones(&self) -> [Bone; 4] {
pub fn bones(&self) -> [BoneRef; 4] {
let bones;
unsafe { bones = &self.__bindgen_anon_1.bones }
[
Bone(&bones[0]),
Bone(&bones[1]),
Bone(&bones[2]),
Bone(&bones[3]),
BoneRef(&bones[0]),
BoneRef(&bones[1]),
BoneRef(&bones[2]),
BoneRef(&bones[3]),
]
}

Expand All @@ -29,22 +29,22 @@ impl<'a> Digit<'a> {
#[doc = " is absent in a real thumb, rather than the metacarpal bone. In the Ultraleap Tracking model,"]
#[doc = " however, we use a \"zero\" metacarpal bone instead for ease of programming."]
#[doc = " @since 3.0.0"]
pub fn metacarpal(&self) -> Bone {
unsafe { Bone(&self.__bindgen_anon_1.__bindgen_anon_1.metacarpal) }
pub fn metacarpal(&self) -> BoneRef {
unsafe { BoneRef(&self.__bindgen_anon_1.__bindgen_anon_1.metacarpal) }
}

#[doc = " The phalange extending from the knuckle. @since 3.0.0"]
pub fn proximal(&self) -> Bone {
unsafe { Bone(&self.__bindgen_anon_1.__bindgen_anon_1.proximal) }
pub fn proximal(&self) -> BoneRef {
unsafe { BoneRef(&self.__bindgen_anon_1.__bindgen_anon_1.proximal) }
}

pub fn intermediate(&self) -> Bone {
unsafe { Bone(&self.__bindgen_anon_1.__bindgen_anon_1.intermediate) }
pub fn intermediate(&self) -> BoneRef {
unsafe { BoneRef(&self.__bindgen_anon_1.__bindgen_anon_1.intermediate) }
}

#[doc = " The bone between the proximal phalange and the distal phalange. @since 3.0.0"]
pub fn distal(&self) -> Bone {
unsafe { Bone(&self.__bindgen_anon_1.__bindgen_anon_1.distal) }
pub fn distal(&self) -> BoneRef {
unsafe { BoneRef(&self.__bindgen_anon_1.__bindgen_anon_1.distal) }
}

#[doc = " Reports whether the finger is more or less straight. @since 3.0.0"]
Expand Down
6 changes: 3 additions & 3 deletions src/distortion_matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ use leap_sys::LEAP_DISTORTION_MATRIX;
#[doc = ""]
#[doc = " Current devices use a 64x64 point distortion grid."]
#[doc = " @since 3.0.0"]
#[derive(Deref)]
pub struct DistortionMatrix<'a>(pub(crate) &'a LEAP_DISTORTION_MATRIX);
#[derive(Deref, Clone, Copy)]
pub struct DistortionMatrixRef<'a>(pub(crate) &'a LEAP_DISTORTION_MATRIX);

#[doc = " A point in the distortion grid. @since 3.0.0"]
pub struct Point {
pub x: f32,
pub y: f32,
}

impl<'a> DistortionMatrix<'a> {
impl<'a> DistortionMatrixRef<'a> {
#[doc = " A grid of 2D points. @since 3.0.0"]
pub fn matrix(&self) -> [[Point; 64]; 64] {
self.matrix.map(|v| v.map(|p| Point { x: p.x, y: p.y }))
Expand Down
Loading

0 comments on commit 905960a

Please sign in to comment.