Skip to content

Commit

Permalink
add AsRef impls for PyBackedStr and PyBackedBytes (#3991)
Browse files Browse the repository at this point in the history
* add `AsRef` impls for `PyBackedStr` and `PyBackedBytes`

* add newsfragment
  • Loading branch information
Icxolu committed Mar 25, 2024
1 parent 7d319a9 commit 54ffaec
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions newsfragments/3991.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
implemented `AsRef<str>` and `AsRef<[u8]>` for `PyBackedStr`, `AsRef<[u8]>` for `PyBackedBytes`.
18 changes: 18 additions & 0 deletions src/pybacked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 54ffaec

Please sign in to comment.