Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ parking_lot = { version = "0.12.3", features = ["arc_lock"] }
cfg-if = "1.0"
once_cell = "1.21"
anyhow = { version = "1", optional = true }
smartstring = { version = "1", optional = true }
ext-php-rs-derive = { version = "=0.11.6", path = "./crates/macros" }

[dev-dependencies]
Expand Down Expand Up @@ -49,6 +50,7 @@ default = ["enum", "runtime"]
closure = []
embed = []
anyhow = ["dep:anyhow"]
smartstring = ["dep:smartstring"]
enum = []
runtime = ["bindgen/runtime"]
static = ["bindgen/static"]
Expand Down
3 changes: 3 additions & 0 deletions src/types/string.rs → src/types/string/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
//! Represents a string in the PHP world. Similar to a C string, but is
//! reference counted and contains the length of the string.

#[cfg(feature = "smartstring")]
mod smartstring_impl;

use std::{
borrow::Cow,
convert::TryFrom,
Expand Down
50 changes: 50 additions & 0 deletions src/types/string/smartstring_impl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use super::{DataType, FromZval, IntoZval, Result, Zval};
use smartstring::{LazyCompact, SmartString};

impl<M: smartstring::SmartStringMode> IntoZval for SmartString<M> {
const TYPE: DataType = DataType::String;
const NULLABLE: bool = false;

fn set_zval(self, zv: &mut Zval, persistent: bool) -> Result<()> {
zv.set_string(self.as_str(), persistent)
}
}

impl FromZval<'_> for SmartString<LazyCompact> {
const TYPE: DataType = DataType::String;

fn from_zval(zval: &Zval) -> Option<Self> {
zval.str().map(SmartString::from)
}
}

#[cfg(test)]
#[cfg(feature = "embed")]
mod tests {
use super::*;
use crate::convert::FromZval;
use crate::embed::Embed;

#[test]
fn test_smartstring_from_zval() {
Embed::run(|| {
let result = Embed::eval("'hello smartstring';");
assert!(result.is_ok());

let zval = result.as_ref().unwrap();
let smart: Option<SmartString<LazyCompact>> = FromZval::from_zval(zval);
assert_eq!(smart, Some(SmartString::from("hello smartstring")));
});
}

#[test]
fn test_smartstring_into_zval() {
Embed::run(|| {
let smart: SmartString<LazyCompact> = SmartString::from("test string");
let zval = smart.into_zval(false).unwrap();

assert!(zval.is_string());
assert_eq!(zval.str(), Some("test string"));
});
}
}
Loading