Struct ext_php_rs::ffi::_zend_string
source · [−]#[repr(C)]pub struct _zend_string {
pub gc: zend_refcounted_h,
pub h: zend_ulong,
pub len: size_t,
pub val: [c_char; 1],
}
Fields
gc: zend_refcounted_h
h: zend_ulong
len: size_t
val: [c_char; 1]
Implementations
sourceimpl _zend_string
impl _zend_string
sourcepub fn new(str: &str, persistent: bool) -> Result<ZBox<Self>>
pub fn new(str: &str, persistent: bool) -> Result<ZBox<Self>>
Creates a new Zend string from a str
.
Parameters
str
- String content.persistent
- Whether the string should persist through the request boundary.
Returns
Returns a result containing the Zend string if successful. Returns an error if the given string contains NUL bytes, which cannot be contained inside a C string.
Panics
Panics if the function was unable to allocate memory for the Zend string.
Safety
When passing persistent
as false
, the caller must ensure that the
object does not attempt to live after the request finishes. When a
request starts and finishes in PHP, the Zend heap is deallocated and a
new one is created, which would leave a dangling pointer in the
ZBox
.
Example
use ext_php_rs::types::ZendStr;
let s = ZendStr::new("Hello, world!", false).unwrap();
sourcepub fn from_c_str(str: &CStr, persistent: bool) -> ZBox<Self>
pub fn from_c_str(str: &CStr, persistent: bool) -> ZBox<Self>
Creates a new Zend string from a CStr
.
Parameters
str
- String content.persistent
- Whether the string should persist through the request boundary.
Panics
Panics if the function was unable to allocate memory for the Zend string.
Safety
When passing persistent
as false
, the caller must ensure that the
object does not attempt to live after the request finishes. When a
request starts and finishes in PHP, the Zend heap is deallocated and a
new one is created, which would leave a dangling pointer in the
ZBox
.
Example
use ext_php_rs::types::ZendStr;
use std::ffi::CString;
let c_s = CString::new("Hello world!").unwrap();
let s = ZendStr::from_c_str(&c_s, false);
sourcepub fn new_interned(str: &str, persistent: bool) -> Result<ZBox<Self>>
pub fn new_interned(str: &str, persistent: bool) -> Result<ZBox<Self>>
Creates a new interned Zend string from a str
.
An interned string is only ever stored once and is immutable. PHP stores the string in an internal hashtable which stores the interned strings.
As Zend hashtables are not thread-safe, a mutex is used to prevent two interned strings from being created at the same time.
Interned strings are not used very often. You should almost always use a regular zend string, except in the case that you know you will use a string that PHP will already have interned, such as “PHP”.
Parameters
str
- String content.persistent
- Whether the string should persist through the request boundary.
Returns
Returns a result containing the Zend string if successful. Returns an error if the given string contains NUL bytes, which cannot be contained inside a C string.
Panics
Panics if the function was unable to allocate memory for the Zend string.
Safety
When passing persistent
as false
, the caller must ensure that the
object does not attempt to live after the request finishes. When a
request starts and finishes in PHP, the Zend heap is deallocated and a
new one is created, which would leave a dangling pointer in the
ZBox
.
Example
use ext_php_rs::types::ZendStr;
let s = ZendStr::new_interned("PHP", true);
sourcepub fn interned_from_c_str(str: &CStr, persistent: bool) -> ZBox<Self>
pub fn interned_from_c_str(str: &CStr, persistent: bool) -> ZBox<Self>
Creates a new interned Zend string from a CStr
.
An interned string is only ever stored once and is immutable. PHP stores the string in an internal hashtable which stores the interned strings.
As Zend hashtables are not thread-safe, a mutex is used to prevent two interned strings from being created at the same time.
Interned strings are not used very often. You should almost always use a regular zend string, except in the case that you know you will use a string that PHP will already have interned, such as “PHP”.
Parameters
str
- String content.persistent
- Whether the string should persist through the request boundary.
Panics
Panics under the following circumstances:
- The function used to create interned strings has not been set.
- The function could not allocate enough memory for the Zend string.
Safety
When passing persistent
as false
, the caller must ensure that the
object does not attempt to live after the request finishes. When a
request starts and finishes in PHP, the Zend heap is deallocated and a
new one is created, which would leave a dangling pointer in the
ZBox
.
Example
use ext_php_rs::types::ZendStr;
use std::ffi::CString;
let c_s = CString::new("PHP").unwrap();
let s = ZendStr::interned_from_c_str(&c_s, true);
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the length of the string.
Example
use ext_php_rs::types::ZendStr;
let s = ZendStr::new("hello, world!", false).unwrap();
assert_eq!(s.len(), 13);
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the string is empty, false otherwise.
Example
use ext_php_rs::types::ZendStr;
let s = ZendStr::new("hello, world!", false).unwrap();
assert_eq!(s.is_empty(), false);
sourcepub fn as_c_str(&self) -> &CStr
pub fn as_c_str(&self) -> &CStr
Returns a reference to the underlying CStr
inside the Zend string.
sourcepub fn as_str(&self) -> Option<&str>
pub fn as_str(&self) -> Option<&str>
Attempts to return a reference to the underlying str
inside the Zend
string.
Returns the None
variant if the CStr
contains non-UTF-8
characters.
Example
use ext_php_rs::types::ZendStr;
let s = ZendStr::new("hello, world!", false).unwrap();
let as_str = s.as_str();
assert_eq!(as_str, Some("hello, world!"));
Trait Implementations
sourceimpl<'a> From<&'a _zend_string> for &'a CStr
impl<'a> From<&'a _zend_string> for &'a CStr
sourceimpl<'a> From<&'a _zend_string> for Cow<'a, ZendStr>
impl<'a> From<&'a _zend_string> for Cow<'a, ZendStr>
sourceimpl PartialEq<_zend_string> for ZendStr
impl PartialEq<_zend_string> for ZendStr
sourceimpl<'a> TryFrom<&'_ _zend_string> for String
impl<'a> TryFrom<&'_ _zend_string> for String
Auto Trait Implementations
impl RefUnwindSafe for _zend_string
impl Send for _zend_string
impl Sync for _zend_string
impl Unpin for _zend_string
impl UnwindSafe for _zend_string
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more