Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/async-grab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ async fn main() -> anyhow::Result<()> {
let pylon = pylon_cxx::Pylon::new();

// Create an instant camera object with the camera device found first.
let mut camera = pylon_cxx::TlFactory::instance(&pylon).create_first_device()?;
let mut camera = pylon_cxx::TlFactory::instance(pylon).create_first_device()?;

// Print the model name of the camera.
println!("Using device {:?}", camera.device_info().model_name()?);
Expand Down
2 changes: 1 addition & 1 deletion examples/chunk-data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ fn main() -> anyhow::Result<()> {
let pylon = pylon_cxx::Pylon::new();

// Create an instant camera object with the camera device found first.
let camera = pylon_cxx::TlFactory::instance(&pylon).create_first_device()?;
let camera = pylon_cxx::TlFactory::instance(pylon).create_first_device()?;

// Print the model name of the camera.
println!("Using device {:?}", camera.device_info().model_name()?);
Expand Down
2 changes: 1 addition & 1 deletion examples/feature-persistence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ fn main() -> anyhow::Result<()> {
let pylon = pylon_cxx::Pylon::new();

// Create an instant camera object with the camera device found first.
let camera = pylon_cxx::TlFactory::instance(&pylon).create_first_device()?;
let camera = pylon_cxx::TlFactory::instance(pylon).create_first_device()?;

// Print the model name of the camera.
println!("Using device {}.", camera.device_info().model_name()?);
Expand Down
2 changes: 1 addition & 1 deletion examples/grab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ fn main() -> anyhow::Result<()> {
let pylon = pylon_cxx::Pylon::new();

// Create an instant camera object with the camera device found first.
let camera = pylon_cxx::TlFactory::instance(&pylon).create_first_device()?;
let camera = pylon_cxx::TlFactory::instance(pylon).create_first_device()?;

// Print the model name of the camera.
println!("Using device {:?}", camera.device_info().model_name()?);
Expand Down
2 changes: 1 addition & 1 deletion examples/reset-all-devices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ fn main() -> anyhow::Result<()> {
// Before using any pylon methods, the pylon runtime must be initialized.
let pylon = pylon_cxx::Pylon::new();

let tl_factory = pylon_cxx::TlFactory::instance(&pylon);
let tl_factory = pylon_cxx::TlFactory::instance(pylon);
for device in tl_factory.enumerate_devices()? {
println!(
"Device {} {} -------------",
Expand Down
2 changes: 1 addition & 1 deletion examples/show-pixel-formats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ fn main() -> anyhow::Result<()> {
let pylon = pylon_cxx::Pylon::new();

// Create an instant camera object with the camera device found first.
let camera = pylon_cxx::TlFactory::instance(&pylon).create_first_device()?;
let camera = pylon_cxx::TlFactory::instance(pylon).create_first_device()?;

camera.open()?;

Expand Down
2 changes: 1 addition & 1 deletion examples/show-properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ fn main() -> anyhow::Result<()> {
// Before using any pylon methods, the pylon runtime must be initialized.
let pylon = pylon_cxx::Pylon::new();

for device in pylon_cxx::TlFactory::instance(&pylon).enumerate_devices()? {
for device in pylon_cxx::TlFactory::instance(pylon).enumerate_devices()? {
println!(
"Device {} {} -------------",
device.property_value("VendorName")?,
Expand Down
53 changes: 28 additions & 25 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ pub use crate::stream_unix as stream;

#[cfg(feature = "stream")]
use std::cell::RefCell;
use std::marker::PhantomData;
use std::sync::Arc;

#[cfg(all(target_os = "windows", feature = "stream"))]
use std::thread::JoinHandle;
Expand Down Expand Up @@ -264,19 +266,20 @@ mod ffi {
pub use ffi::GrabStrategy;
pub use ffi::TimeoutHandling;

pub struct Pylon {}
mod private {
pub struct Private;
}

impl Pylon {
pub fn new() -> Self {
ffi::PylonInitialize();
Self {}
}
pub struct Pylon {
_private_marker: PhantomData<private::Private>,
}

impl Default for Pylon {
fn default() -> Self {
impl Pylon {
pub fn new() -> Arc<Self> {
ffi::PylonInitialize();
Self {}
Arc::new(Self {
_private_marker: PhantomData,
})
}
}

Expand Down Expand Up @@ -325,21 +328,21 @@ pub unsafe fn terminate(shutdown_logging: bool) {
// Since in C++ `CTlFactory::GetInstance()` merely returns a reference to
// a static object, here we don't store anything and instead get the
// reference when needed.
pub struct TlFactory<'a> {
lib: &'a Pylon,
pub struct TlFactory {
lib: Arc<Pylon>,
}

impl<'a> TlFactory<'a> {
pub fn instance(lib: &'a Pylon) -> Self {
impl TlFactory {
pub fn instance(lib: Arc<Pylon>) -> Self {
Self { lib }
}
pub fn create_first_device(&self) -> PylonResult<InstantCamera<'a>> {
pub fn create_first_device(&self) -> PylonResult<InstantCamera> {
let inner = ffi::tl_factory_create_first_device()?;
Ok(InstantCamera::new(self.lib, inner))
Ok(InstantCamera::new(self.lib.clone(), inner))
}
pub fn create_device(&self, device_info: &DeviceInfo) -> PylonResult<InstantCamera<'a>> {
pub fn create_device(&self, device_info: &DeviceInfo) -> PylonResult<InstantCamera> {
let inner = ffi::tl_factory_create_device(&device_info.inner)?;
Ok(InstantCamera::new(self.lib, inner))
Ok(InstantCamera::new(self.lib.clone(), inner))
}
pub fn enumerate_devices(&self) -> PylonResult<Vec<DeviceInfo>> {
let devs: cxx::UniquePtr<cxx::CxxVector<ffi::CDeviceInfo>> =
Expand Down Expand Up @@ -367,16 +370,16 @@ impl WaitObject {
unsafe impl Send for WaitObject {}

/// Wrap the CInstantCamera type
pub struct InstantCamera<'a> {
pub struct InstantCamera {
inner: cxx::UniquePtr<ffi::CInstantCamera>,
#[cfg(all(not(target_os = "windows"), feature = "stream"))]
fd: RefCell<Option<tokio::io::unix::AsyncFd<std::os::unix::io::RawFd>>>,
#[cfg(all(target_os = "windows", feature = "stream"))]
wait_thread: RefCell<Option<JoinHandle<()>>>,

/// A reference to the Pylon library. This should be the last field in the
/// struct so that `self._lib` is dropped after `self.inner`.
_lib: &'a Pylon,
#[allow(dead_code)]
lib: Arc<Pylon>,
}

/// Wrap the `GenApi::INodeMap` type.
Expand Down Expand Up @@ -578,17 +581,17 @@ impl CommandNode {
}
}

unsafe impl Send for InstantCamera<'_> {}
unsafe impl Send for InstantCamera {}

impl<'a> InstantCamera<'a> {
pub fn new(lib: &'a Pylon, inner: cxx::UniquePtr<ffi::CInstantCamera>) -> Self {
impl InstantCamera {
pub fn new(lib: Arc<Pylon>, inner: cxx::UniquePtr<ffi::CInstantCamera>) -> Self {
InstantCamera {
_lib: lib,
inner,
#[cfg(all(not(target_os = "windows"), feature = "stream"))]
fd: RefCell::new(None),
#[cfg(all(target_os = "windows", feature = "stream"))]
wait_thread: RefCell::new(None),
lib,
}
}

Expand Down Expand Up @@ -683,7 +686,7 @@ impl<'a> InstantCamera<'a> {
}

/// These methods return the various node maps.
impl<'a> InstantCamera<'a> {
impl<'a> InstantCamera {
pub fn node_map<'map>(&'a self) -> PylonResult<NodeMap<'map, 'a>> {
Ok(NodeMap {
inner: ffi::instant_camera_get_node_map(&self.inner)?,
Expand Down
2 changes: 1 addition & 1 deletion src/stream_unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::pin::Pin;
use std::task::{Context, Poll};
use tokio_stream::Stream;

impl<'a> Stream for InstantCamera<'a> {
impl Stream for InstantCamera {
type Item = GrabResult;

fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<GrabResult>> {
Expand Down
2 changes: 1 addition & 1 deletion src/stream_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
use crate::{GrabResult, InstantCamera, TimeoutHandling};
use tokio_stream::Stream;

impl Stream for InstantCamera<'_> {
impl Stream for InstantCamera {
type Item = GrabResult;

fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<GrabResult>> {
Expand Down
8 changes: 4 additions & 4 deletions tests/async_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use tokio_stream::{StreamExt, StreamMap};
async fn streaming_works() -> PylonResult<()> {
let mut images = 10;
let pylon = Pylon::new();
let mut cam = TlFactory::instance(&pylon).create_first_device()?;
let mut cam = TlFactory::instance(pylon).create_first_device()?;
cam.open()?;
cam.start_grabbing(&GrabOptions::default().count(images))?;
while let Some(res) = cam.next().await {
Expand All @@ -21,12 +21,12 @@ async fn streaming_works() -> PylonResult<()> {
async fn streaming_all_cams_works() -> PylonResult<()> {
let pylon = Pylon::new();
let mut streams = StreamMap::new();
TlFactory::instance(&pylon)
TlFactory::instance(pylon.clone())
.enumerate_devices()?
.iter()
.enumerate()
.try_for_each(|(n, info)| {
let cam = TlFactory::instance(&pylon).create_device(info)?;
let cam = TlFactory::instance(pylon.clone()).create_device(info)?;
cam.open()?;
cam.start_grabbing(&GrabOptions::default())?;
streams.insert(format!("{}-{}", info.model_name()?, n), Box::pin(cam));
Expand All @@ -45,7 +45,7 @@ async fn streaming_all_cams_works() -> PylonResult<()> {
#[tokio::test]
async fn start_stop_loop_works() -> PylonResult<()> {
let pylon = Pylon::new();
let mut cam = TlFactory::instance(&pylon).create_first_device()?;
let mut cam = TlFactory::instance(pylon).create_first_device()?;
cam.open()?;
for _ in 0..5 {
let mut images = 10;
Expand Down