add `AsRef` impls for `PyBackedStr` and `PyBackedBytes` (#3991)

* add `AsRef` impls for `PyBackedStr` and `PyBackedBytes`

* add newsfragment
This commit is contained in:
Icxolu 2024-03-25 17:21:27 +01:00 committed by GitHub
parent 7d319a906e
commit 54ffaecd65
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 0 deletions

View File

@ -0,0 +1 @@
implemented `AsRef<str>` and `AsRef<[u8]>` for `PyBackedStr`, `AsRef<[u8]>` for `PyBackedBytes`.

View File

@ -27,6 +27,18 @@ impl Deref for PyBackedStr {
} }
} }
impl AsRef<str> for PyBackedStr {
fn as_ref(&self) -> &str {
self
}
}
impl AsRef<[u8]> for PyBackedStr {
fn as_ref(&self) -> &[u8] {
self.as_bytes()
}
}
impl TryFrom<Bound<'_, PyString>> for PyBackedStr { impl TryFrom<Bound<'_, PyString>> for PyBackedStr {
type Error = PyErr; type Error = PyErr;
fn try_from(py_string: Bound<'_, PyString>) -> Result<Self, Self::Error> { fn try_from(py_string: Bound<'_, PyString>) -> Result<Self, Self::Error> {
@ -82,6 +94,12 @@ impl Deref for PyBackedBytes {
} }
} }
impl AsRef<[u8]> for PyBackedBytes {
fn as_ref(&self) -> &[u8] {
self
}
}
impl From<Bound<'_, PyBytes>> for PyBackedBytes { impl From<Bound<'_, PyBytes>> for PyBackedBytes {
fn from(py_bytes: Bound<'_, PyBytes>) -> Self { fn from(py_bytes: Bound<'_, PyBytes>) -> Self {
let b = py_bytes.as_bytes(); let b = py_bytes.as_bytes();