roplat 0.2.0

roplat: just a robot operation system
Documentation
//! 多语言类型绑定特征
//!
//! 用于描述 Rust 类型在目标语言中的类型映射关系。
//! 该模块为基础类型和三缓冲通讯类型提供默认实现,
//! 透明/不透明消息由过程宏自动生成实现。

use crate::comm::{Publisher, Subscriber, TripleBufferChannel};
use crate::comm::{RingBufferChannel, RingBufferReader, RingBufferWriter};

/// C++ 语言标记
pub struct CppLang;

/// Python 语言标记
pub struct PyLang;

/// 类型绑定特征
pub trait TypeBinding<L> {
    /// 目标语言中的类型名
    fn target_type_name() -> &'static str;

    /// 是否为不透明类型
    fn is_opaque() -> bool {
        false
    }
}

macro_rules! impl_primitive_binding {
    ($rust_ty:ty, $cpp_ty:expr, $py_ty:expr) => {
        impl TypeBinding<CppLang> for $rust_ty {
            fn target_type_name() -> &'static str {
                $cpp_ty
            }
        }

        impl TypeBinding<PyLang> for $rust_ty {
            fn target_type_name() -> &'static str {
                $py_ty
            }
        }
    };
}

impl_primitive_binding!(f32, "float", "float");
impl_primitive_binding!(f64, "double", "float");
impl_primitive_binding!(i8, "int8_t", "int");
impl_primitive_binding!(i16, "int16_t", "int");
impl_primitive_binding!(i32, "int32_t", "int");
impl_primitive_binding!(i64, "int64_t", "int");
impl_primitive_binding!(u8, "uint8_t", "int");
impl_primitive_binding!(u16, "uint16_t", "int");
impl_primitive_binding!(u32, "uint32_t", "int");
impl_primitive_binding!(u64, "uint64_t", "int");
impl_primitive_binding!(bool, "bool", "bool");
impl_primitive_binding!(usize, "size_t", "int");
impl_primitive_binding!(isize, "intptr_t", "int");

/// Publisher 绑定到其 payload 类型
impl<L, T> TypeBinding<L> for Publisher<T>
where
    T: Clone + TypeBinding<L>,
{
    fn target_type_name() -> &'static str {
        T::target_type_name()
    }

    fn is_opaque() -> bool {
        T::is_opaque()
    }
}

/// Subscriber 绑定到其 payload 类型
impl<L, T> TypeBinding<L> for Subscriber<T>
where
    T: TypeBinding<L>,
{
    fn target_type_name() -> &'static str {
        T::target_type_name()
    }

    fn is_opaque() -> bool {
        T::is_opaque()
    }
}

/// TripleBufferChannel 绑定到其 payload 类型
impl<L, T> TypeBinding<L> for TripleBufferChannel<T>
where
    T: Clone + TypeBinding<L>,
{
    fn target_type_name() -> &'static str {
        T::target_type_name()
    }

    fn is_opaque() -> bool {
        T::is_opaque()
    }
}

/// RingBufferWriter 绑定到其 payload 类型
impl<L, T> TypeBinding<L> for RingBufferWriter<T>
where
    T: TypeBinding<L>,
{
    fn target_type_name() -> &'static str {
        T::target_type_name()
    }

    fn is_opaque() -> bool {
        T::is_opaque()
    }
}

/// RingBufferReader 绑定到其 payload 类型
impl<L, T> TypeBinding<L> for RingBufferReader<T>
where
    T: TypeBinding<L>,
{
    fn target_type_name() -> &'static str {
        T::target_type_name()
    }

    fn is_opaque() -> bool {
        T::is_opaque()
    }
}

/// RingBufferChannel 绑定到其 payload 类型
impl<L, T> TypeBinding<L> for RingBufferChannel<T>
where
    T: Send + TypeBinding<L>,
{
    fn target_type_name() -> &'static str {
        T::target_type_name()
    }

    fn is_opaque() -> bool {
        T::is_opaque()
    }
}