ext_php_rs/describe/
abi.rs1use std::{fmt::Display, ops::Deref, vec::Vec as StdVec};
17
18#[repr(C)]
20pub struct Vec<T> {
21 ptr: *mut T,
22 len: usize,
23}
24
25impl<T> Deref for Vec<T> {
26 type Target = [T];
27
28 fn deref(&self) -> &Self::Target {
29 unsafe { std::slice::from_raw_parts(self.ptr, self.len) }
30 }
31}
32
33impl<T> Drop for Vec<T> {
34 fn drop(&mut self) {
35 unsafe {
36 let _ = Box::from_raw(std::ptr::slice_from_raw_parts_mut(self.ptr, self.len));
37 };
38 }
39}
40
41impl<T> From<StdVec<T>> for Vec<T> {
42 fn from(vec: StdVec<T>) -> Self {
43 let vec = vec.into_boxed_slice();
44 let len = vec.len();
45 let ptr = Box::into_raw(vec) as *mut T;
46
47 Self { ptr, len }
48 }
49}
50
51#[repr(C)]
53pub struct Str {
54 ptr: *const u8,
55 len: usize,
56}
57
58impl Str {
59 pub fn str(&self) -> &'static str {
64 unsafe { std::str::from_utf8_unchecked(std::slice::from_raw_parts(self.ptr, self.len)) }
65 }
66}
67
68impl From<&'static str> for Str {
69 fn from(val: &'static str) -> Self {
70 let ptr = val.as_ptr();
71 let len = val.len();
72 Self { ptr, len }
73 }
74}
75
76impl AsRef<str> for Str {
77 fn as_ref(&self) -> &str {
78 self.str()
79 }
80}
81
82impl Display for Str {
83 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
84 self.str().fmt(f)
85 }
86}
87
88#[repr(C, u8)]
90pub enum Option<T> {
91 Some(T),
92 None,
93}