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 #[inline]
120 #[must_use]
121 pub fn as_str(&self) -> &str {
122 std::str::from_utf8(&self.inner).expect("RString value is not valid UTF-8")
123 }
124}
125
126impl From<&str> for RString {
127 fn from(s: &str) -> Self {
128 Self {
129 inner: s.as_bytes().to_vec().into(),
130 }
131 }
132}
133
134impl From<String> for RString {
135 fn from(s: String) -> Self {
136 Self {
137 inner: s.into_bytes().into(),
138 }
139 }
140}
141
142impl AsRef<str> for RString {
143 fn as_ref(&self) -> &str {
144 self.as_str()
145 }
146}
147
148impl Display for RString {
149 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
150 self.as_str().fmt(f)
151 }
152}
153
154#[repr(C, u8)]
156#[derive(Debug)]
157pub enum Option<T> {
158 Some(T),
160 None,
162}
163
164impl<T> From<std::option::Option<T>> for Option<T> {
165 fn from(opt: std::option::Option<T>) -> Self {
166 match opt {
167 Some(val) => Self::Some(val),
168 None => Self::None,
169 }
170 }
171}
172
173impl<T> PartialEq for Option<T>
174where
175 T: PartialEq,
176{
177 fn eq(&self, other: &Self) -> bool {
178 match (self, other) {
179 (Self::Some(a), Self::Some(b)) => a == b,
180 (Self::None, Self::None) => true,
181 _ => false,
182 }
183 }
184}