In ext-php-rs, binary data (typically generated via PHP's pack or unpack functions) is represented as a string in PHP. You can use BinarySlice<T> in Rust to treat these PHP strings as read-only slices of type T without copying the underlying data.
Requirements
To use BinarySlice<T>, the type T must implement the PackSlice trait. This is currently implemented for most primitive numbers:
- Signed integers:
i8, i16, i32, i64, isize - Unsigned integers:
u8, u16, u32, u64, usize - Floats:
f32, f64
Mapping Table
T parameter | &T parameter | T Return type | &T Return type | PHP representation |
|---|
| Yes | No | No | No | zend_string |
Implementation Detail
The data is converted into a slice, and the pointer to the data is set as the string pointer, with the length of the array being the length of the string.
#![cfg_attr(windows, feature(abi_vectorcall))]
extern crate ext_php_rs;
use ext_php_rs::prelude::*;
use ext_php_rs::binary_slice::BinarySlice;
#[php_function]
pub fn test_binary_slice(input: BinarySlice<u8>) -> u8 {
let mut sum = 0;
for i in input.iter() {
sum += i;
}
sum
}
fn main() {}