c3ne-types 0.1.0

c3ne-types is a helper crate for users of the main c3ne crate that provides an implementation of the messier C3 types for use in Rust.
Documentation
use std::{
    error::Error,
    ffi::{CStr, CString, c_void},
    mem::forget,
};

#[repr(C)]
/// A standard C3 String. It holds a constant pointer to a `c_void` and an `usize` holding the length of the string.
pub struct C3String {
    ptr: *const c_void,
    len: usize,
}

impl C3String {
    fn get_ptr<P>(value: P) -> (*const i8, usize)
    where
        P: Into<String>,
    {
        let value = value.into() as String;
        let cstring = CString::new(value.clone()).expect("Unable to convert String to CString");

        let ptr = cstring.as_ptr();
        let len = value.len();

        forget(cstring);

        (ptr, len)
    }

    fn str_from_ptr(ptr: *mut i8) -> Result<&'static str, Box<dyn Error>> {
        let cstring = unsafe { CStr::from_ptr(ptr) };
        let string = cstring.to_str()?;

        Ok(string)
    }

    pub fn ptr(&self) -> *const c_void {
        self.ptr
    }

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

    /// Converts a given String or slice into a C3String.
    ///
    /// Example:
    /// ```rs
    /// c3ne_types::C3String::into_c3_string("Hello, World!");
    /// ```
    pub fn into_c3_string<P>(value: P) -> Self
    where
        P: Into<String>,
    {
        let (ptr, len) = Self::get_ptr(value);
        Self {
            ptr: ptr as *const c_void,
            len: len,
        }
    }

    /// Converts a given C3String into a string slice.
    ///
    /// Example:
    /// ```rs
    /// let c3_string = c3ne_types::C3String::into_c3_string("Hello, World!");
    /// assert!("Hello, World!", c3_string.str_from_c3_string().unwrap());
    /// ```
    pub fn str_from_c3_string(&self) -> Result<&'static str, Box<dyn Error>> {
        Self::str_from_ptr(self.ptr.cast_mut() as *mut i8)
    }

    /// Converts a given String or slice into a C3ZString.
    ///
    /// Example:
    /// ```rs
    /// c3ne_types::C3String::into_c3_zstring("Hello, World!");
    /// ```
    pub fn into_c3_zstring<P>(value: P) -> C3ZString
    where
        P: Into<String>,
    {
        Self::get_ptr(value).0 as *const u8
    }

    /// Converts a given C3ZString into a string slice.
    ///
    /// Example:
    /// ```rs
    /// let c3_zstring = c3ne_types::C3String::into_c3_zstring("Hello, World!");
    /// assert!("Hello, World!", c3ne_types::CString::str_from_c3_zstring(c3_zstring));
    /// ```
    pub fn str_from_c3_zstring(ptr: C3ZString) -> Result<&'static str, Box<dyn Error>> {
        Self::str_from_ptr(ptr.cast_mut() as *mut i8)
    }

    /// Converts a given String or slice into a C3WString.
    ///
    /// Example:
    /// ```rs
    /// c3ne_types::C3String::into_c3_wstring("Hello, World!");
    /// ```
    pub fn into_c3_wstring<P>(value: P) -> C3WString
    where
        P: Into<String>,
    {
        Self::get_ptr(value).0 as *const u16
    }

    /// Converts a given C3WString into a string slice.
    ///
    /// Example:
    /// ```rs
    /// let c3_wstring = c3ne_types::C3String::into_c3_wstring("Hello, World!");
    /// assert!("Hello, World!", c3ne_types::CString::str_from_c3_wstring(c3_wstring));
    /// ```
    pub fn str_from_c3_wstring(ptr: C3WString) -> Result<&'static str, Box<dyn Error>> {
        Self::str_from_ptr(ptr.cast_mut() as *mut i8)
    }

    /// Converts a given String or slice into a C3DString.
    ///
    /// Example:
    /// ```rs
    /// c3ne_types::C3String::into_c3_dstring("Hello, World!");
    /// ```
    pub fn into_c3_dstring<P>(value: P) -> C3DString
    where
        P: Into<String>,
    {
        Self::get_ptr(value).0 as *const c_void
    }

    /// Converts a given C3DString into a string slice.
    ///
    /// Example:
    /// ```rs
    /// let c3_dstring = c3ne_types::C3String::into_c3_dstring("Hello, World!");
    /// assert!("Hello, World!", c3ne_types::CString::str_from_c3_dstring(c3_dstring));
    /// ```
    pub fn str_from_c3_dstring(ptr: C3DString) -> Result<&'static str, Box<dyn Error>> {
        Self::str_from_ptr(ptr.cast_mut() as *mut i8)
    }
}

/// A String designed for C interoperability, where null-terminated strings are expected.
pub type C3ZString = *const u8;
/// Similar to [C3ZString] but uses `ui6` instead of `u8` for UTF-16 support.
pub type C3WString = *const u16;
/// A dynamic string builder.
pub type C3DString = *const c_void;