Skip to content

Commit

Permalink
noice!
Browse files Browse the repository at this point in the history
  • Loading branch information
jessekrubin committed Jan 19, 2024
1 parent 4144340 commit adf9e51
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
51 changes: 51 additions & 0 deletions crates/ryo3/src/anystr.rs
Original file line number Diff line number Diff line change
@@ -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<PyObject> 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<AnyStr> {
Ok(s)
}

#[pyfunction]
pub fn string_noop(s: String) -> PyResult<String> {
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::<AnyStr>()?;
Ok(())
}
2 changes: 2 additions & 0 deletions crates/ryo3/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use pyo3::prelude::PyModule;
use pyo3::{PyResult, Python};

pub mod anystr;
pub mod fmts;
pub mod fs;
pub mod sh;
Expand All @@ -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(())
}

0 comments on commit adf9e51

Please sign in to comment.