#[repr(C)]
pub struct _zend_string { pub gc: zend_refcounted_h, pub h: zend_ulong, pub len: usize, pub val: [c_char; 1], }

Fields§

§gc: zend_refcounted_h§h: zend_ulong§len: usize§val: [c_char; 1]

Implementations§

source§

impl _zend_string

source

pub fn new(str: impl AsRef<[u8]>, persistent: bool) -> ZBox<Self>

Creates a new Zend string from a slice of bytes.

§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;

let s = ZendStr::new("Hello, world!", false);
let php = ZendStr::new([80, 72, 80], false);
source

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);
source

pub fn new_interned(str: impl AsRef<[u8]>, persistent: bool) -> ZBox<Self>

Creates a new interned Zend string from a slice of bytes.

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;

let s = ZendStr::new_interned("PHP", true);
source

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);
source

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);
assert_eq!(s.len(), 13);
source

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);
assert_eq!(s.is_empty(), false);
source

pub fn as_c_str(&self) -> Result<&CStr>

Attempts to return a reference to the underlying bytes inside the Zend string as a CStr.

Returns an Error::InvalidCString variant if the string contains null bytes.

source

pub fn as_str(&self) -> Result<&str>

Attempts to return a reference to the underlying bytes inside the Zend string.

Returns an Error::InvalidUtf8 variant if the str contains non-UTF-8 characters.

§Example
use ext_php_rs::types::ZendStr;

let s = ZendStr::new("hello, world!", false);
assert!(s.as_str().is_ok());
source

pub fn as_bytes(&self) -> &[u8]

Returns a reference to the underlying bytes inside the Zend string.

source

pub fn as_ptr(&self) -> *const ZendStr

Returns a raw pointer to this object

source

pub fn as_mut_ptr(&mut self) -> *mut ZendStr

Returns a mutable pointer to this object

Trait Implementations§

source§

impl<'a> From<&'a _zend_string> for Cow<'a, ZendStr>

source§

fn from(value: &'a ZendStr) -> Self

Converts to this type from the input type.
source§

impl<'a> TryFrom<&'a _zend_string> for &'a CStr

§

type Error = Error

The type returned in the event of a conversion error.
source§

fn try_from(value: &'a ZendStr) -> Result<Self>

Performs the conversion.
source§

impl<'a> TryFrom<&'a _zend_string> for &'a str

§

type Error = Error

The type returned in the event of a conversion error.
source§

fn try_from(value: &'a ZendStr) -> Result<Self>

Performs the conversion.
source§

impl TryFrom<&_zend_string> for String

§

type Error = Error

The type returned in the event of a conversion error.
source§

fn try_from(value: &ZendStr) -> Result<Self>

Performs the conversion.

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.