diff --git a/src/types/mod.rs b/src/types/mod.rs index effe9b80..96a442ce 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -174,6 +174,5 @@ mod sequence; mod set; mod slice; mod string; -mod stringutils; mod tuple; mod typeobject; diff --git a/src/types/string.rs b/src/types/string.rs index 90685abf..3847724c 100644 --- a/src/types/string.rs +++ b/src/types/string.rs @@ -1,5 +1,7 @@ // Copyright (c) 2017-present PyO3 Project and Contributors +use crate::conversion::FromPyObject; +use crate::conversion::{IntoPyObject, PyTryFrom, ToPyObject}; use crate::err::{PyErr, PyResult}; use crate::exceptions; use crate::ffi; @@ -113,6 +115,87 @@ impl PyBytes { } } +/// Converts Rust `str` to Python object. +/// See `PyString::new` for details on the conversion. +impl ToPyObject for str { + #[inline] + fn to_object(&self, py: Python) -> PyObject { + PyString::new(py, self).into() + } +} + +impl<'a> IntoPyObject for &'a str { + #[inline] + fn into_object(self, py: Python) -> PyObject { + PyString::new(py, self).into() + } +} + +/// Converts Rust `Cow` to Python object. +/// See `PyString::new` for details on the conversion. +impl<'a> ToPyObject for Cow<'a, str> { + #[inline] + fn to_object(&self, py: Python) -> PyObject { + PyString::new(py, self).into() + } +} + +/// Converts Rust `String` to Python object. +/// See `PyString::new` for details on the conversion. +impl ToPyObject for String { + #[inline] + fn to_object(&self, py: Python) -> PyObject { + PyString::new(py, self).into() + } +} + +impl IntoPyObject for String { + #[inline] + fn into_object(self, py: Python) -> PyObject { + PyString::new(py, &self).into() + } +} + +impl<'a> IntoPyObject for &'a String { + #[inline] + fn into_object(self, py: Python) -> PyObject { + PyString::new(py, self).into() + } +} + +/// Allows extracting strings from Python objects. +/// Accepts Python `str` and `unicode` objects. +impl<'source> crate::FromPyObject<'source> for Cow<'source, str> { + fn extract(ob: &'source PyAny) -> PyResult { + ::try_from(ob)?.to_string() + } +} + +/// Allows extracting strings from Python objects. +/// Accepts Python `str` and `unicode` objects. +impl<'a> crate::FromPyObject<'a> for &'a str { + fn extract(ob: &'a PyAny) -> PyResult { + let s: Cow<'a, str> = crate::FromPyObject::extract(ob)?; + match s { + Cow::Borrowed(r) => Ok(r), + Cow::Owned(r) => { + let r = ob.py().register_any(r); + Ok(r.as_str()) + } + } + } +} + +/// Allows extracting strings from Python objects. +/// Accepts Python `str` and `unicode` objects. +impl<'source> FromPyObject<'source> for String { + fn extract(obj: &'source PyAny) -> PyResult { + ::try_from(obj)? + .to_string() + .map(Cow::into_owned) + } +} + #[cfg(test)] mod test { use super::PyString; diff --git a/src/types/stringutils.rs b/src/types/stringutils.rs deleted file mode 100644 index 87959ed4..00000000 --- a/src/types/stringutils.rs +++ /dev/null @@ -1,89 +0,0 @@ -use crate::err::PyResult; -use crate::instance::PyNativeType; -use crate::object::PyObject; -use crate::types::{PyAny, PyString}; -use crate::FromPyObject; -use crate::Python; -use crate::{IntoPyObject, PyTryFrom, ToPyObject}; -use std::borrow::Cow; - -/// Converts Rust `str` to Python object. -/// See `PyString::new` for details on the conversion. -impl ToPyObject for str { - #[inline] - fn to_object(&self, py: Python) -> PyObject { - PyString::new(py, self).into() - } -} - -impl<'a> IntoPyObject for &'a str { - #[inline] - fn into_object(self, py: Python) -> PyObject { - PyString::new(py, self).into() - } -} - -/// Converts Rust `Cow` to Python object. -/// See `PyString::new` for details on the conversion. -impl<'a> ToPyObject for Cow<'a, str> { - #[inline] - fn to_object(&self, py: Python) -> PyObject { - PyString::new(py, self).into() - } -} - -/// Converts Rust `String` to Python object. -/// See `PyString::new` for details on the conversion. -impl ToPyObject for String { - #[inline] - fn to_object(&self, py: Python) -> PyObject { - PyString::new(py, self).into() - } -} - -impl IntoPyObject for String { - #[inline] - fn into_object(self, py: Python) -> PyObject { - PyString::new(py, &self).into() - } -} - -impl<'a> IntoPyObject for &'a String { - #[inline] - fn into_object(self, py: Python) -> PyObject { - PyString::new(py, self).into() - } -} - -/// Allows extracting strings from Python objects. -/// Accepts Python `str` and `unicode` objects. -impl<'source> crate::FromPyObject<'source> for Cow<'source, str> { - fn extract(ob: &'source PyAny) -> PyResult { - ::try_from(ob)?.to_string() - } -} - -/// Allows extracting strings from Python objects. -/// Accepts Python `str` and `unicode` objects. -impl<'a> crate::FromPyObject<'a> for &'a str { - fn extract(ob: &'a PyAny) -> PyResult { - let s: Cow<'a, str> = crate::FromPyObject::extract(ob)?; - match s { - Cow::Borrowed(r) => Ok(r), - Cow::Owned(r) => { - let r = ob.py().register_any(r); - Ok(r.as_str()) - } - } - } -} - -/// Allows extracting strings from Python objects. -/// Accepts Python `str` and `unicode` objects. -impl<'source> FromPyObject<'source> for String { - fn extract(obj: &'source PyAny) -> PyResult { - ::try_from(obj)? - .to_string() - .map(Cow::into_owned) - } -}