Skip to main content

rustolio_utils/bytes/
generic_bytes.rs

1//
2// SPDX-License-Identifier: MPL-2.0
3//
4// Copyright (c) 2026 Tobias Binnewies. All rights reserved.
5//
6// This Source Code Form is subject to the terms of the Mozilla Public
7// License, v. 2.0. If a copy of the MPL was not distributed with this
8// file, You can obtain one at http://mozilla.org/MPL/2.0/.
9//
10
11use bytes::{Buf, Bytes};
12
13pub struct GenericBytes(Box<dyn Buf + Send + Sync + 'static>);
14
15impl Default for GenericBytes {
16    fn default() -> Self {
17        Self::new(Bytes::new())
18    }
19}
20
21impl GenericBytes {
22    pub fn new(b: impl Buf + Send + Sync + 'static) -> Self {
23        Self(Box::new(b))
24    }
25}
26
27impl Buf for GenericBytes {
28    fn remaining(&self) -> usize {
29        self.0.remaining()
30    }
31
32    fn advance(&mut self, cnt: usize) {
33        self.0.advance(cnt);
34    }
35    fn chunk(&self) -> &[u8] {
36        self.0.chunk()
37    }
38}