ext_php_rs/describe/
abi.rs1use std::{fmt::Display, ops::Deref, vec::Vec as StdVec};
17
18#[repr(C)]
20#[derive(Debug)]
21pub struct Vec<T> {
22    ptr: *mut T,
23    len: usize,
24}
25
26impl<T> Deref for Vec<T> {
27    type Target = [T];
28
29    fn deref(&self) -> &Self::Target {
30        unsafe { std::slice::from_raw_parts(self.ptr, self.len) }
31    }
32}
33
34impl<T> Drop for Vec<T> {
35    fn drop(&mut self) {
36        unsafe {
37            let _ = Box::from_raw(std::ptr::slice_from_raw_parts_mut(self.ptr, self.len));
38        };
39    }
40}
41
42impl<T> From<StdVec<T>> for Vec<T> {
43    fn from(vec: StdVec<T>) -> Self {
44        let vec = vec.into_boxed_slice();
45        let len = vec.len();
46        let ptr = Box::into_raw(vec).cast::<T>();
47
48        Self { ptr, len }
49    }
50}
51
52impl<T> PartialEq for Vec<T>
53where
54    T: PartialEq,
55{
56    fn eq(&self, other: &Self) -> bool {
57        self.len == other.len && self.as_ref() == other.as_ref()
58    }
59}
60
61#[repr(C)]
63#[derive(Debug)]
64pub struct Str {
65    ptr: *const u8,
66    len: usize,
67}
68
69impl Str {
70    #[must_use]
75    pub fn str(&self) -> &'static str {
76        unsafe { std::str::from_utf8_unchecked(std::slice::from_raw_parts(self.ptr, self.len)) }
77    }
78}
79
80impl From<&'static str> for Str {
81    fn from(val: &'static str) -> Self {
82        let ptr = val.as_ptr();
83        let len = val.len();
84        Self { ptr, len }
85    }
86}
87
88impl AsRef<str> for Str {
89    fn as_ref(&self) -> &str {
90        self.str()
91    }
92}
93
94impl Display for Str {
95    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
96        self.str().fmt(f)
97    }
98}
99
100impl PartialEq for Str {
101    fn eq(&self, other: &Self) -> bool {
102        self.len == other.len && self.str() == other.str()
103    }
104}
105
106#[repr(C)]
108#[derive(Debug, PartialEq)]
109pub struct RString {
110    inner: Vec<u8>,
111}
112
113impl RString {
114    #[must_use]
120    pub fn as_str(&self) -> &str {
121        std::str::from_utf8(&self.inner).expect("RString value is not valid UTF-8")
122    }
123}
124
125impl From<&str> for RString {
126    fn from(s: &str) -> Self {
127        Self {
128            inner: s.as_bytes().to_vec().into(),
129        }
130    }
131}
132
133impl From<String> for RString {
134    fn from(s: String) -> Self {
135        Self {
136            inner: s.into_bytes().into(),
137        }
138    }
139}
140
141impl AsRef<str> for RString {
142    fn as_ref(&self) -> &str {
143        self.as_str()
144    }
145}
146
147impl Display for RString {
148    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
149        self.as_str().fmt(f)
150    }
151}
152
153#[repr(C, u8)]
155#[derive(Debug)]
156pub enum Option<T> {
157    Some(T),
159    None,
161}
162
163impl<T> From<std::option::Option<T>> for Option<T> {
164    fn from(opt: std::option::Option<T>) -> Self {
165        match opt {
166            Some(val) => Self::Some(val),
167            None => Self::None,
168        }
169    }
170}
171
172impl<T> PartialEq for Option<T>
173where
174    T: PartialEq,
175{
176    fn eq(&self, other: &Self) -> bool {
177        match (self, other) {
178            (Self::Some(a), Self::Some(b)) => a == b,
179            (Self::None, Self::None) => true,
180            _ => false,
181        }
182    }
183}