Skip to main content

miniextendr_api/
markers.rs

1//! Marker traits for proc-macro derived types.
2//!
3//! These marker traits identify types that have been derived with specific proc-macros.
4//! They enable compile-time type checking and blanket implementations.
5//!
6//! # Pattern
7//!
8//! Each derive macro generates an impl of its corresponding marker trait:
9//!
10//! | Derive Macro | Marker Trait |
11//! |--------------|--------------|
12//! | `#[derive(PreferList)]` | [`crate::markers::PrefersList`] |
13//! | `#[derive(PreferExternalPtr)]` | [`crate::markers::PrefersExternalPtr`] |
14//! | `#[derive(PreferRNativeType)]` | [`crate::markers::PrefersRNativeType`] |
15//! | `#[derive(PreferDataFrame)]` | [`crate::markers::PrefersDataFrame`] |
16
17/// Marker trait for types that should be converted to R lists via `IntoR`.
18///
19/// Implemented by the `PreferList` derive; you can also implement it manually.
20pub trait PrefersList: crate::list::IntoList {}
21
22/// Marker trait for types that should be converted to R data frames via `IntoR`.
23///
24/// Implemented by the `PreferDataFrame` derive; you can also implement it manually.
25pub trait PrefersDataFrame: crate::convert::IntoDataFrame {}
26
27/// Marker trait for types that prefer `ExternalPtr` conversion.
28///
29/// Implemented by the `PreferExternalPtr` derive; currently informational.
30pub trait PrefersExternalPtr: crate::externalptr::IntoExternalPtr {}
31
32/// Marker trait for types that prefer native SEXP conversion.
33///
34/// Implemented by the `PreferRNativeType` derive; currently informational.
35pub trait PrefersRNativeType: crate::ffi::RNativeType {}
36
37// region: Coercion marker traits
38
39/// Marker trait for types that can widen to `i32` without loss.
40///
41/// Manually implemented for specific types to avoid conflicts with identity/
42/// special-case conversions. Used by blanket Coerce implementations.
43pub trait WidensToI32: Into<i32> + Copy {}
44
45/// Marker trait for types that can widen to `f64` without loss.
46///
47/// Manually implemented for specific types to avoid conflicts with identity/
48/// special-case conversions. Used by blanket Coerce implementations.
49pub trait WidensToF64: Into<f64> + Copy {}
50
51// Explicit marker impls for widening conversions (no blanket impl to avoid conflicts)
52impl WidensToI32 for i8 {}
53impl WidensToI32 for i16 {}
54impl WidensToI32 for u8 {}
55impl WidensToI32 for u16 {}
56
57impl WidensToF64 for f32 {}
58impl WidensToF64 for i8 {}
59impl WidensToF64 for i16 {}
60impl WidensToF64 for i32 {}
61impl WidensToF64 for u8 {}
62impl WidensToF64 for u16 {}
63impl WidensToF64 for u32 {}
64// endregion