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 {
type Error = PyErr;
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 {
fn from(py_bytes: Bound<'_, PyBytes>) -> Self {
let b = py_bytes.as_bytes();