| 1 |
//! PyO3 integration for CompactString |
| 2 |
//! |
| 3 |
//! This module provides seamless conversion between `CompactString` and Python strings, |
| 4 |
//! allowing `CompactString` to be used in PyO3-based Python extensions. |
| 5 |
|
| 6 |
use pyo3::prelude::*; |
| 7 |
use pyo3::types::PyString; |
| 8 |
|
| 9 |
use crate::CompactString; |
| 10 |
|
| 11 |
#[cfg_attr(docsrs, doc(cfg(feature = "pyo3")))] |
| 12 |
impl<'a, 'py> FromPyObject<'a, 'py> for CompactString { |
| 13 |
type Error = PyErr; |
| 14 |
|
| 15 |
fn extract(obj: pyo3::Borrowed<'a, 'py, PyAny>) -> PyResult<Self> { |
| 16 |
let s: &str = obj.extract()?; |
| 17 |
Ok(CompactString::from(s)) |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
#[cfg_attr(docsrs, doc(cfg(feature = "pyo3")))] |
| 22 |
impl<'py> IntoPyObject<'py> for CompactString { |
| 23 |
type Target = PyString; |
| 24 |
type Output = Bound<'py, Self::Target>; |
| 25 |
type Error = core::convert::Infallible; |
| 26 |
|
| 27 |
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> { |
| 28 |
Ok(PyString::new(py, self.as_str())) |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
#[cfg_attr(docsrs, doc(cfg(feature = "pyo3")))] |
| 33 |
impl<'py> IntoPyObject<'py> for &CompactString { |
| 34 |
type Target = PyString; |
| 35 |
type Output = Bound<'py, Self::Target>; |
| 36 |
type Error = core::convert::Infallible; |
| 37 |
|
| 38 |
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> { |
| 39 |
Ok(PyString::new(py, self.as_str())) |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
// These tests initialize an embedded Python interpreter and call into libpython via FFI. |
| 44 |
// Miri cannot execute foreign functions, so they're excluded under Miri (the impl above is |
| 45 |
// still checked by Miri; only these FFI-driven tests are skipped). |
| 46 |
#[cfg(all(test, not(miri)))] |
| 47 |
mod tests { |
| 48 |
use pyo3::prelude::*; |
| 49 |
use pyo3::types::PyString; |
| 50 |
|
| 51 |
use crate::CompactString; |
| 52 |
|
| 53 |
fn with_py<F, R>(f: F) -> R |
| 54 |
where |
| 55 |
F: for<'py> FnOnce(Python<'py>) -> R, |
| 56 |
{ |
| 57 |
Python::initialize(); |
| 58 |
Python::attach(f) |
| 59 |
} |
| 60 |
|
| 61 |
#[test] |
| 62 |
fn test_compact_string_into_py() { |
| 63 |
with_py(|py| { |
| 64 |
let cs = CompactString::from("hello world"); |
| 65 |
let py_str = cs.into_pyobject(py).unwrap(); |
| 66 |
assert_eq!(py_str.to_str().unwrap(), "hello world"); |
| 67 |
}); |
| 68 |
} |
| 69 |
|
| 70 |
#[test] |
| 71 |
fn test_compact_string_ref_into_py() { |
| 72 |
with_py(|py| { |
| 73 |
let cs = CompactString::from("hello world"); |
| 74 |
let py_str = (&cs).into_pyobject(py).unwrap(); |
| 75 |
assert_eq!(py_str.to_str().unwrap(), "hello world"); |
| 76 |
// Original is still valid |
| 77 |
assert_eq!(cs.as_str(), "hello world"); |
| 78 |
}); |
| 79 |
} |
| 80 |
|
| 81 |
#[test] |
| 82 |
fn test_compact_string_from_py() { |
| 83 |
with_py(|py| { |
| 84 |
let py_str = PyString::new(py, "hello from python"); |
| 85 |
let cs: CompactString = py_str.extract().unwrap(); |
| 86 |
assert_eq!(cs.as_str(), "hello from python"); |
| 87 |
}); |
| 88 |
} |
| 89 |
|
| 90 |
#[test] |
| 91 |
fn test_roundtrip() { |
| 92 |
with_py(|py| { |
| 93 |
let original = CompactString::from("roundtrip test 🦀"); |
| 94 |
let py_str = original.clone().into_pyobject(py).unwrap(); |
| 95 |
let recovered: CompactString = py_str.extract().unwrap(); |
| 96 |
assert_eq!(original, recovered); |
| 97 |
}); |
| 98 |
} |
| 99 |
|
| 100 |
#[test] |
| 101 |
fn test_empty_string() { |
| 102 |
with_py(|py| { |
| 103 |
let cs = CompactString::new(""); |
| 104 |
let py_str = cs.into_pyobject(py).unwrap(); |
| 105 |
assert_eq!(py_str.to_str().unwrap(), ""); |
| 106 |
|
| 107 |
let py_empty = PyString::new(py, ""); |
| 108 |
let cs_empty: CompactString = py_empty.extract().unwrap(); |
| 109 |
assert!(cs_empty.is_empty()); |
| 110 |
}); |
| 111 |
} |
| 112 |
|
| 113 |
#[test] |
| 114 |
fn test_unicode_string() { |
| 115 |
with_py(|py| { |
| 116 |
let unicode_str = "Hello, 世界! 🎉 Привет мир"; |
| 117 |
let cs = CompactString::from(unicode_str); |
| 118 |
let py_str = cs.into_pyobject(py).unwrap(); |
| 119 |
assert_eq!(py_str.to_str().unwrap(), unicode_str); |
| 120 |
|
| 121 |
let recovered: CompactString = py_str.extract().unwrap(); |
| 122 |
assert_eq!(recovered.as_str(), unicode_str); |
| 123 |
}); |
| 124 |
} |
| 125 |
|
| 126 |
#[test] |
| 127 |
fn test_inline_string() { |
| 128 |
with_py(|py| { |
| 129 |
// Short string that should be stored inline (not on heap) |
| 130 |
let short = CompactString::from("hi"); |
| 131 |
assert!(!short.is_heap_allocated()); |
| 132 |
|
| 133 |
let py_str = short.clone().into_pyobject(py).unwrap(); |
| 134 |
let recovered: CompactString = py_str.extract().unwrap(); |
| 135 |
assert_eq!(short, recovered); |
| 136 |
}); |
| 137 |
} |
| 138 |
|
| 139 |
#[test] |
| 140 |
fn test_heap_string() { |
| 141 |
with_py(|py| { |
| 142 |
// Long string that should be stored on the heap |
| 143 |
let long = CompactString::from("this is a longer string that won't fit inline"); |
| 144 |
assert!(long.is_heap_allocated()); |
| 145 |
|
| 146 |
let py_str = long.clone().into_pyobject(py).unwrap(); |
| 147 |
let recovered: CompactString = py_str.extract().unwrap(); |
| 148 |
assert_eq!(long, recovered); |
| 149 |
}); |
| 150 |
} |
| 151 |
} |