Trait ext_php_rs::convert::IntoZval
source · pub trait IntoZval: Sized {
const TYPE: DataType;
fn set_zval(self, zv: &mut Zval, persistent: bool) -> Result<()>;
fn into_zval(self, persistent: bool) -> Result<Zval> { ... }
}
Expand description
Required Associated Constants§
Required Methods§
sourcefn set_zval(self, zv: &mut Zval, persistent: bool) -> Result<()>
fn set_zval(self, zv: &mut Zval, persistent: bool) -> Result<()>
Sets the content of a pre-existing zval. Returns a result containing nothing if setting the content was successful.
Parameters
zv
- The Zval to set the content of.persistent
- Whether the contents of the Zval will persist between requests.
Provided Methods§
sourcefn into_zval(self, persistent: bool) -> Result<Zval>
fn into_zval(self, persistent: bool) -> Result<Zval>
Converts a Rust primitive type into a Zval. Returns a result containing the Zval if successful.
Parameters
persistent
- Whether the contents of the Zval will persist between requests.
Examples found in repository?
More examples
src/types/array.rs (line 314)
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
pub fn insert<V>(&mut self, key: &str, val: V) -> Result<()>
where
V: IntoZval,
{
let mut val = val.into_zval(false)?;
unsafe { zend_hash_str_update(self, CString::new(key)?.as_ptr(), key.len(), &mut val) };
val.release();
Ok(())
}
/// Inserts an item into the hash table at a specified index, or updates if
/// the key already exists. Returns nothing in a result if successful.
///
/// # Parameters
///
/// * `key` - The index at which the value should be inserted.
/// * `val` - The value to insert into the hash table.
///
/// # Returns
///
/// Returns nothing in a result on success. Returns an error if converting
/// the value into a [`Zval`] failed.
///
/// # Example
///
/// ```no_run
/// use ext_php_rs::types::ZendHashTable;
///
/// let mut ht = ZendHashTable::new();
///
/// ht.insert_at_index(0, "A");
/// ht.insert_at_index(5, "B");
/// ht.insert_at_index(0, "C"); // notice overriding index 0
/// assert_eq!(ht.len(), 2);
/// ```
pub fn insert_at_index<V>(&mut self, key: u64, val: V) -> Result<()>
where
V: IntoZval,
{
let mut val = val.into_zval(false)?;
unsafe { zend_hash_index_update(self, key, &mut val) };
val.release();
Ok(())
}
/// Pushes an item onto the end of the hash table. Returns a result
/// containing nothing if the element was successfully inserted.
///
/// # Parameters
///
/// * `val` - The value to insert into the hash table.
///
/// # Returns
///
/// Returns nothing in a result on success. Returns an error if converting
/// the value into a [`Zval`] failed.
///
/// # Example
///
/// ```no_run
/// use ext_php_rs::types::ZendHashTable;
///
/// let mut ht = ZendHashTable::new();
///
/// ht.push("a");
/// ht.push("b");
/// ht.push("c");
/// assert_eq!(ht.len(), 3);
/// ```
pub fn push<V>(&mut self, val: V) -> Result<()>
where
V: IntoZval,
{
let mut val = val.into_zval(false)?;
unsafe { zend_hash_next_index_insert(self, &mut val) };
val.release();
Ok(())
}
src/builders/class.rs (line 113)
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
pub fn property<T: Into<String>>(
mut self,
name: T,
default: impl IntoZval,
flags: PropertyFlags,
) -> Self {
let default = match default.into_zval(true) {
Ok(default) => default,
Err(_) => panic!("Invalid default value for property `{}`.", name.into()),
};
self.properties.push((name.into(), default, flags));
self
}
/// Adds a constant to the class. The type of the constant is defined by the
/// type of the given default.
///
/// Returns a result containing the class builder if the constant was
/// successfully added.
///
/// # Parameters
///
/// * `name` - The name of the constant to add to the class.
/// * `value` - The value of the constant.
pub fn constant<T: Into<String>>(mut self, name: T, value: impl IntoZval) -> Result<Self> {
let value = value.into_zval(true)?;
self.constants.push((name.into(), value));
Ok(self)
}
src/types/object.rs (line 166)
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
pub fn set_property(&mut self, name: &str, value: impl IntoZval) -> Result<()> {
let mut name = ZendStr::new(name, false);
let mut value = value.into_zval(false)?;
unsafe {
self.handlers()?.write_property.ok_or(Error::InvalidScope)?(
self,
name.deref_mut(),
&mut value,
std::ptr::null_mut(),
)
.as_ref()
}
.ok_or(Error::InvalidScope)?;
Ok(())
}