labview_interop/memory/
mod.rs

1//! The memory module handles the LabVIEW memory manager
2//! functions and types.
3//!
4//! todo: get to reference without panics.
5#[cfg(feature = "link")]
6mod owned_handle;
7mod uhandle;
8mod uptr;
9
10use std::fmt::Debug;
11
12/// A trait which defines that a type should be copyable inside
13/// of a LabVIEW handle.
14///
15/// This is unique from `Copy` since unsized types can be inside a handle
16/// but they can't implement `Copy`.
17pub trait LVCopy {}
18
19/// Rust copy types should be copyable in LabVIEW.
20impl<T: Copy> LVCopy for T {}
21
22/// Magic cookie type used for various reference types in the memory manager.
23#[derive(Clone, Copy, PartialEq, Eq, Debug)]
24#[repr(transparent)]
25#[doc(hidden)]
26pub struct MagicCookie(u32);
27
28#[cfg(feature = "link")]
29pub use owned_handle::OwnedUHandle;
30pub use uhandle::UHandle;
31pub use uptr::UPtr;
32
33/// Extracted formatting logic which can be used for handles or owned values.
34fn fmt_handle<T: Debug + ?Sized>(
35    label: &str,
36    handle: &UHandle<T>,
37    f: &mut std::fmt::Formatter<'_>,
38) -> std::fmt::Result {
39    match unsafe { handle.as_ref() } {
40        Ok(inner) => write!(f, "{label}({inner:?})"),
41        Err(_) => write!(f, "{label}(Invalid)"),
42    }
43}