Skip to content

Commit 54ffaec

Browse files
authored
add AsRef impls for PyBackedStr and PyBackedBytes (#3991)
* add `AsRef` impls for `PyBackedStr` and `PyBackedBytes` * add newsfragment
1 parent 7d319a9 commit 54ffaec

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

newsfragments/3991.added.md

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

src/pybacked.rs

+18
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@ impl Deref for PyBackedStr {
2727
}
2828
}
2929

30+
impl AsRef<str> for PyBackedStr {
31+
fn as_ref(&self) -> &str {
32+
self
33+
}
34+
}
35+
36+
impl AsRef<[u8]> for PyBackedStr {
37+
fn as_ref(&self) -> &[u8] {
38+
self.as_bytes()
39+
}
40+
}
41+
3042
impl TryFrom<Bound<'_, PyString>> for PyBackedStr {
3143
type Error = PyErr;
3244
fn try_from(py_string: Bound<'_, PyString>) -> Result<Self, Self::Error> {
@@ -82,6 +94,12 @@ impl Deref for PyBackedBytes {
8294
}
8395
}
8496

97+
impl AsRef<[u8]> for PyBackedBytes {
98+
fn as_ref(&self) -> &[u8] {
99+
self
100+
}
101+
}
102+
85103
impl From<Bound<'_, PyBytes>> for PyBackedBytes {
86104
fn from(py_bytes: Bound<'_, PyBytes>) -> Self {
87105
let b = py_bytes.as_bytes();

0 commit comments

Comments
 (0)