safe_drive 0.4.3

safe_drive: Formally Specified Rust Bindings for ROS2
Documentation
// This file was automatically generated by ros2msg_to_rs (https://github.com/tier4/ros2msg_to_rs).
use super::super::super::*;
use super::*;
use safe_drive::msg::common_interfaces::*;
use safe_drive::msg::*;
use safe_drive::rcl;

extern "C" {
    fn example_msg__msg__Num__init(msg: *mut Num) -> bool;
    fn example_msg__msg__Num__fini(msg: *mut Num);
    fn example_msg__msg__Num__are_equal(lhs: *const Num, rhs: *const Num) -> bool;
    fn example_msg__msg__Num__Sequence__init(msg: *mut NumSeqRaw, size: usize) -> bool;
    fn example_msg__msg__Num__Sequence__fini(msg: *mut NumSeqRaw);
    fn example_msg__msg__Num__Sequence__are_equal(
        lhs: *const NumSeqRaw,
        rhs: *const NumSeqRaw,
    ) -> bool;
    fn rosidl_typesupport_c__get_message_type_support_handle__example_msg__msg__Num(
    ) -> *const rcl::rosidl_message_type_support_t;
}

#[repr(C)]
#[derive(Debug)]
pub struct Num {
    pub num: i64,
}

impl Num {
    pub fn new() -> Option<Self> {
        let mut msg: Self = unsafe { std::mem::MaybeUninit::zeroed().assume_init() };
        if unsafe { example_msg__msg__Num__init(&mut msg) } {
            Some(msg)
        } else {
            None
        }
    }
}

impl Drop for Num {
    fn drop(&mut self) {
        unsafe { example_msg__msg__Num__fini(self) };
    }
}

#[repr(C)]
#[derive(Debug)]
struct NumSeqRaw {
    data: *mut Num,
    size: size_t,
    capacity: size_t,
}

/// Sequence of Num.
/// `N` is the maximum number of elements.
/// If `N` is `0`, the size is unlimited.
#[repr(C)]
#[derive(Debug)]
pub struct NumSeq<const N: usize> {
    data: *mut Num,
    size: size_t,
    capacity: size_t,
}

impl<const N: usize> NumSeq<N> {
    /// Create a sequence of.
    /// `N` represents the maximum number of elements.
    /// If `N` is `0`, the sequence is unlimited.
    pub fn new(size: usize) -> Option<Self> {
        if N != 0 && size > N {
            // the size exceeds in the maximum number
            return None;
        }

        let mut msg: NumSeqRaw = unsafe { std::mem::MaybeUninit::zeroed().assume_init() };
        if unsafe { example_msg__msg__Num__Sequence__init(&mut msg, size) } {
            Some(Self {
                data: msg.data,
                size: msg.size,
                capacity: msg.capacity,
            })
        } else {
            None
        }
    }

    pub fn null() -> Self {
        let msg: NumSeqRaw = unsafe { std::mem::MaybeUninit::zeroed().assume_init() };
        Self {
            data: msg.data,
            size: msg.size,
            capacity: msg.capacity,
        }
    }

    pub fn as_slice(&self) -> &[Num] {
        if self.data.is_null() {
            &[]
        } else {
            let s = unsafe { std::slice::from_raw_parts(self.data, self.size as _) };
            s
        }
    }

    pub fn as_slice_mut(&mut self) -> &mut [Num] {
        if self.data.is_null() {
            &mut []
        } else {
            let s = unsafe { std::slice::from_raw_parts_mut(self.data, self.size as _) };
            s
        }
    }

    pub fn iter(&self) -> std::slice::Iter<'_, Num> {
        self.as_slice().iter()
    }

    pub fn iter_mut(&mut self) -> std::slice::IterMut<'_, Num> {
        self.as_slice_mut().iter_mut()
    }

    pub fn len(&self) -> usize {
        self.as_slice().len()
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

impl<const N: usize> Drop for NumSeq<N> {
    fn drop(&mut self) {
        let mut msg = NumSeqRaw {
            data: self.data,
            size: self.size,
            capacity: self.capacity,
        };
        unsafe { example_msg__msg__Num__Sequence__fini(&mut msg) };
    }
}

unsafe impl<const N: usize> Send for NumSeq<N> {}
unsafe impl<const N: usize> Sync for NumSeq<N> {}

impl TypeSupport for Num {
    fn type_support() -> *const rcl::rosidl_message_type_support_t {
        unsafe { rosidl_typesupport_c__get_message_type_support_handle__example_msg__msg__Num() }
    }
}

impl PartialEq for Num {
    fn eq(&self, other: &Self) -> bool {
        unsafe { example_msg__msg__Num__are_equal(self, other) }
    }
}

impl<const N: usize> PartialEq for NumSeq<N> {
    fn eq(&self, other: &Self) -> bool {
        unsafe {
            let msg1 = NumSeqRaw {
                data: self.data,
                size: self.size,
                capacity: self.capacity,
            };
            let msg2 = NumSeqRaw {
                data: other.data,
                size: other.size,
                capacity: other.capacity,
            };
            example_msg__msg__Num__Sequence__are_equal(&msg1, &msg2)
        }
    }
}