Skip to content

Commit ee8a1d5

Browse files
Merge pull request #42 from triblespace/codex/implement-trait-for-anybytes-support
Implement Bytes integration with bytes crate
2 parents 5157093 + ade6ec8 commit ee8a1d5

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@
6767
dereferences to `[u8]`
6868
- documented creating `Bytes` from `Arc` sources without an extra wrapper and
6969
removed the corresponding task from the inventory
70+
- implemented `bytes::Buf` for `Bytes` and `From<Bytes>` for `bytes::Bytes` for
71+
seamless integration with Tokio and other libraries
7072

7173
## 0.19.3 - 2025-05-30
7274
- implemented `Error` for `ViewError`

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "anybytes"
3-
version = "0.19.3"
3+
version = "0.19.4"
44
edition = "2021"
55
license = "MIT"
66
repository = "https://github.com/triblespace/anybytes"

src/bytes.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,13 @@ impl<T: ByteSource + ByteOwner> From<Arc<T>> for Bytes {
433433
}
434434
}
435435

436+
#[cfg(feature = "bytes")]
437+
impl From<Bytes> for bytes::Bytes {
438+
fn from(bytes: Bytes) -> Self {
439+
bytes::Bytes::from_owner(bytes)
440+
}
441+
}
442+
436443
impl Deref for Bytes {
437444
type Target = [u8];
438445
#[inline]
@@ -501,6 +508,27 @@ impl fmt::Debug for Bytes {
501508
}
502509
}
503510

511+
#[cfg(feature = "bytes")]
512+
impl bytes::Buf for Bytes {
513+
#[inline]
514+
fn remaining(&self) -> usize {
515+
self.data.len()
516+
}
517+
518+
#[inline]
519+
fn chunk(&self) -> &[u8] {
520+
self.data
521+
}
522+
523+
#[inline]
524+
fn advance(&mut self, cnt: usize) {
525+
if cnt > self.data.len() {
526+
panic!("advance out of bounds: {} > {}", cnt, self.data.len());
527+
}
528+
self.data = &self.data[cnt..];
529+
}
530+
}
531+
504532
#[cfg(test)]
505533
mod tests {
506534
use super::Bytes;

0 commit comments

Comments
 (0)