From adf9e51d9652601d8d316c9371fcd8ec8d0736e3 Mon Sep 17 00:00:00 2001 From: jessekrubin Date: Fri, 19 Jan 2024 09:57:59 -0800 Subject: [PATCH] noice! --- crates/ryo3/src/anystr.rs | 51 +++++++++++++++++++++++++++++++++++++++ crates/ryo3/src/lib.rs | 2 ++ 2 files changed, 53 insertions(+) create mode 100644 crates/ryo3/src/anystr.rs diff --git a/crates/ryo3/src/anystr.rs b/crates/ryo3/src/anystr.rs new file mode 100644 index 0000000..cbe94e5 --- /dev/null +++ b/crates/ryo3/src/anystr.rs @@ -0,0 +1,51 @@ +/// Rust version of python's typing.AnyStr +/// TBD if this is a good idea or not :/ +use std::fmt::Debug; + +use pyo3::prelude::*; +use pyo3::types::PyBytes; +use pyo3::PyNativeType; +use pyo3::PyTypeInfo; + +#[derive(Debug, FromPyObject)] +pub enum AnyStr<'a> { + Str(String), + Bytes(&'a PyBytes), +} + +impl IntoPy for AnyStr<'_> { + fn into_py(self, py: Python) -> PyObject { + match self { + AnyStr::Str(s) => s.into_py(py), + + AnyStr::Bytes(b) => { + // b.into() + b.into_py(py) + // PyBytes::new(py, &b).into() + } + } + } +} + +#[pyfunction] +pub fn anystr_noop(s: AnyStr) -> PyResult { + Ok(s) +} + +#[pyfunction] +pub fn string_noop(s: String) -> PyResult { + Ok(s) +} + +#[pyfunction] +pub fn bytes_noop<'a>(py: Python<'a>, b: &'a PyBytes) -> PyResult<&'a PyBytes> { + Ok(b) +} + +pub fn madd(_py: Python, m: &PyModule) -> PyResult<()> { + m.add_function(wrap_pyfunction!(anystr_noop, m)?)?; + m.add_function(wrap_pyfunction!(string_noop, m)?)?; + m.add_function(wrap_pyfunction!(bytes_noop, m)?)?; + // m.add_class::()?; + Ok(()) +} diff --git a/crates/ryo3/src/lib.rs b/crates/ryo3/src/lib.rs index 900e978..40139b4 100644 --- a/crates/ryo3/src/lib.rs +++ b/crates/ryo3/src/lib.rs @@ -1,6 +1,7 @@ use pyo3::prelude::PyModule; use pyo3::{PyResult, Python}; +pub mod anystr; pub mod fmts; pub mod fs; pub mod sh; @@ -17,5 +18,6 @@ pub fn madd(py: Python, m: &PyModule) -> PyResult<()> { sh::madd(m)?; fs::pymod(m)?; sp::madd(py, m)?; + anystr::madd(py, m)?; Ok(()) }