Skip to content

Commit

Permalink
feat: add ZBytes.to_bytearray
Browse files Browse the repository at this point in the history
It allows to avoid a copy if you want to get an additional copy, as
otherwise you would have to first get a `bytes` instance, then create
a bytearray from it.
  • Loading branch information
wyfo committed Nov 15, 2024
1 parent 3d43728 commit 0a4f28c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ impl ZBytes {
self.0.to_bytes()
}

fn to_bytearray<'py>(&self, py: Python<'py>) -> Bound<'py, PyByteArray> {
PyByteArray::new_bound(py, &self.0.to_bytes())
}

fn to_string(&self) -> PyResult<Cow<str>> {
self.0
.try_to_string()
Expand Down
1 change: 1 addition & 0 deletions zenoh/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,7 @@ class ZBytes:

def __new__(cls, bytes: bytearray | bytes | str = None) -> Self: ...
def to_bytes(self) -> bytes: ...
def to_bytearray(self) -> bytearray: ...
def to_string(self) -> str: ...
def __bool__(self) -> bool: ...
def __len__(self) -> int: ...
Expand Down

0 comments on commit 0a4f28c

Please sign in to comment.