c3ne_types/lib.rs
1use std::{
2 error::Error,
3 ffi::{CStr, CString, c_void},
4 mem::forget,
5};
6
7#[repr(C)]
8/// A standard C3 String. It holds a constant pointer to a `c_void` and an `usize` holding the length of the string.
9pub struct C3String {
10 ptr: *const c_void,
11 len: usize,
12}
13
14impl C3String {
15 fn get_ptr<P>(value: P) -> (*const i8, usize)
16 where
17 P: Into<String>,
18 {
19 let value = value.into() as String;
20 let cstring = CString::new(value.clone()).expect("Unable to convert String to CString");
21
22 let ptr = cstring.as_ptr();
23 let len = value.len();
24
25 forget(cstring);
26
27 (ptr, len)
28 }
29
30 fn str_from_ptr(ptr: *mut i8) -> Result<&'static str, Box<dyn Error>> {
31 let cstring = unsafe { CStr::from_ptr(ptr) };
32 let string = cstring.to_str()?;
33
34 Ok(string)
35 }
36
37 pub fn ptr(&self) -> *const c_void {
38 self.ptr
39 }
40
41 pub fn len(&self) -> usize {
42 self.len
43 }
44
45 /// Converts a given String or slice into a C3String.
46 ///
47 /// Example:
48 /// ```rs
49 /// c3ne_types::C3String::into_c3_string("Hello, World!");
50 /// ```
51 pub fn into_c3_string<P>(value: P) -> Self
52 where
53 P: Into<String>,
54 {
55 let (ptr, len) = Self::get_ptr(value);
56 Self {
57 ptr: ptr as *const c_void,
58 len: len,
59 }
60 }
61
62 /// Converts a given C3String into a string slice.
63 ///
64 /// Example:
65 /// ```rs
66 /// let c3_string = c3ne_types::C3String::into_c3_string("Hello, World!");
67 /// assert!("Hello, World!", c3_string.str_from_c3_string().unwrap());
68 /// ```
69 pub fn str_from_c3_string(&self) -> Result<&'static str, Box<dyn Error>> {
70 Self::str_from_ptr(self.ptr.cast_mut() as *mut i8)
71 }
72
73 /// Converts a given String or slice into a C3ZString.
74 ///
75 /// Example:
76 /// ```rs
77 /// c3ne_types::C3String::into_c3_zstring("Hello, World!");
78 /// ```
79 pub fn into_c3_zstring<P>(value: P) -> C3ZString
80 where
81 P: Into<String>,
82 {
83 Self::get_ptr(value).0 as *const u8
84 }
85
86 /// Converts a given C3ZString into a string slice.
87 ///
88 /// Example:
89 /// ```rs
90 /// let c3_zstring = c3ne_types::C3String::into_c3_zstring("Hello, World!");
91 /// assert!("Hello, World!", c3ne_types::CString::str_from_c3_zstring(c3_zstring));
92 /// ```
93 pub fn str_from_c3_zstring(ptr: C3ZString) -> Result<&'static str, Box<dyn Error>> {
94 Self::str_from_ptr(ptr.cast_mut() as *mut i8)
95 }
96
97 /// Converts a given String or slice into a C3WString.
98 ///
99 /// Example:
100 /// ```rs
101 /// c3ne_types::C3String::into_c3_wstring("Hello, World!");
102 /// ```
103 pub fn into_c3_wstring<P>(value: P) -> C3WString
104 where
105 P: Into<String>,
106 {
107 Self::get_ptr(value).0 as *const u16
108 }
109
110 /// Converts a given C3WString into a string slice.
111 ///
112 /// Example:
113 /// ```rs
114 /// let c3_wstring = c3ne_types::C3String::into_c3_wstring("Hello, World!");
115 /// assert!("Hello, World!", c3ne_types::CString::str_from_c3_wstring(c3_wstring));
116 /// ```
117 pub fn str_from_c3_wstring(ptr: C3WString) -> Result<&'static str, Box<dyn Error>> {
118 Self::str_from_ptr(ptr.cast_mut() as *mut i8)
119 }
120
121 /// Converts a given String or slice into a C3DString.
122 ///
123 /// Example:
124 /// ```rs
125 /// c3ne_types::C3String::into_c3_dstring("Hello, World!");
126 /// ```
127 pub fn into_c3_dstring<P>(value: P) -> C3DString
128 where
129 P: Into<String>,
130 {
131 Self::get_ptr(value).0 as *const c_void
132 }
133
134 /// Converts a given C3DString into a string slice.
135 ///
136 /// Example:
137 /// ```rs
138 /// let c3_dstring = c3ne_types::C3String::into_c3_dstring("Hello, World!");
139 /// assert!("Hello, World!", c3ne_types::CString::str_from_c3_dstring(c3_dstring));
140 /// ```
141 pub fn str_from_c3_dstring(ptr: C3DString) -> Result<&'static str, Box<dyn Error>> {
142 Self::str_from_ptr(ptr.cast_mut() as *mut i8)
143 }
144}
145
146/// A String designed for C interoperability, where null-terminated strings are expected.
147pub type C3ZString = *const u8;
148/// Similar to [C3ZString] but uses `ui6` instead of `u8` for UTF-16 support.
149pub type C3WString = *const u16;
150/// A dynamic string builder.
151pub type C3DString = *const c_void;
152