ext_php_rs/zend/
streams.rs1use std::ptr::{self, NonNull};
2
3use crate::{
4 error::Error,
5 ffi::{
6 php_register_url_stream_wrapper, php_register_url_stream_wrapper_volatile, php_stream,
7 php_stream_context, php_stream_locate_url_wrapper, php_stream_wrapper,
8 php_stream_wrapper_ops, php_unregister_url_stream_wrapper,
9 php_unregister_url_stream_wrapper_volatile, zend_string,
10 },
11 types::ZendStr,
12};
13
14pub type StreamWrapper = php_stream_wrapper;
16
17pub type StreamOpener = unsafe extern "C" fn(
19 *mut StreamWrapper,
20 *const std::ffi::c_char,
21 *const std::ffi::c_char,
22 i32,
23 *mut *mut zend_string,
24 *mut php_stream_context,
25 i32,
26 *const std::ffi::c_char,
27 u32,
28 *const std::ffi::c_char,
29 u32,
30) -> *mut Stream;
31
32impl StreamWrapper {
33 #[must_use]
35 pub fn get(name: &str) -> Option<&Self> {
36 unsafe {
37 let result = php_stream_locate_url_wrapper(name.as_ptr().cast(), ptr::null_mut(), 0);
38 Some(NonNull::new(result)?.as_ref())
39 }
40 }
41
42 #[must_use]
44 #[allow(clippy::mut_from_ref)]
45 pub fn get_mut(name: &str) -> Option<&mut Self> {
46 unsafe {
47 let result = php_stream_locate_url_wrapper(name.as_ptr().cast(), ptr::null_mut(), 0);
48 Some(NonNull::new(result)?.as_mut())
49 }
50 }
51
52 pub fn register(self, name: &str) -> Result<Self, Error> {
63 let copy = Box::new(self);
65 let copy = Box::leak(copy);
66 let name = std::ffi::CString::new(name).expect("Could not create C string for name!");
67 let result = unsafe { php_register_url_stream_wrapper(name.as_ptr(), copy) };
68 if result == 0 {
69 Ok(*copy)
70 } else {
71 Err(Error::StreamWrapperRegistrationFailure)
72 }
73 }
74
75 pub fn register_volatile(self, name: &str) -> Result<Self, Error> {
82 let copy = Box::new(self);
84 let copy = Box::leak(copy);
85 let name = ZendStr::new(name, false);
86 let result =
87 unsafe { php_register_url_stream_wrapper_volatile((*name).as_ptr().cast_mut(), copy) };
88 if result == 0 {
89 Ok(*copy)
90 } else {
91 Err(Error::StreamWrapperRegistrationFailure)
92 }
93 }
94
95 pub fn unregister(name: &str) -> Result<(), Error> {
106 let name = std::ffi::CString::new(name).expect("Could not create C string for name!");
107 match unsafe { php_unregister_url_stream_wrapper(name.as_ptr()) } {
108 0 => Ok(()),
109 _ => Err(Error::StreamWrapperUnregistrationFailure),
110 }
111 }
112
113 pub fn unregister_volatile(name: &str) -> Result<(), Error> {
120 let name = ZendStr::new(name, false);
121 match unsafe { php_unregister_url_stream_wrapper_volatile((*name).as_ptr().cast_mut()) } {
122 0 => Ok(()),
123 _ => Err(Error::StreamWrapperUnregistrationFailure),
124 }
125 }
126
127 #[must_use]
129 pub fn wops(&self) -> &php_stream_wrapper_ops {
130 unsafe { &*self.wops }
131 }
132
133 pub fn wops_mut(&mut self) -> &mut php_stream_wrapper_ops {
135 unsafe { &mut *(self.wops.cast_mut()) }
136 }
137}
138
139pub type Stream = php_stream;
141
142pub type StreamWrapperOps = php_stream_wrapper_ops;
144
145impl StreamWrapperOps {}