blob: 61848ad9c3c2cb9074885d70f3809388026b0dec [file] [log] [blame]
Jooyung Han12a0b702021-08-05 23:20:31 +09001/*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//! Provides extension methods Bytes::read<T>(), which calls back ReadFromBytes::read_from_byte()
18
19use anyhow::{bail, Result};
20use bytes::{Buf, Bytes};
21use std::ops::Deref;
22
23#[derive(Clone)]
24pub struct LengthPrefixed<T> {
25 inner: T,
26}
27
28impl<T> Deref for LengthPrefixed<T> {
29 type Target = T;
30 fn deref(&self) -> &Self::Target {
31 &self.inner
32 }
33}
34
35pub trait BytesExt {
36 fn read<T: ReadFromBytes>(&mut self) -> Result<T>;
37}
38
39impl BytesExt for Bytes {
40 fn read<T: ReadFromBytes>(&mut self) -> Result<T> {
41 T::read_from_bytes(self)
42 }
43}
44
45pub trait ReadFromBytes {
46 fn read_from_bytes(buf: &mut Bytes) -> Result<Self>
47 where
48 Self: Sized;
49}
50
51impl ReadFromBytes for u32 {
52 fn read_from_bytes(buf: &mut Bytes) -> Result<Self> {
53 Ok(buf.get_u32_le())
54 }
55}
56
57impl<T: ReadFromBytes> ReadFromBytes for Vec<T> {
58 fn read_from_bytes(buf: &mut Bytes) -> Result<Self> {
59 let mut result = vec![];
60 while buf.has_remaining() {
61 result.push(buf.read()?);
62 }
63 Ok(result)
64 }
65}
66
67impl<T: ReadFromBytes> ReadFromBytes for LengthPrefixed<T> {
68 fn read_from_bytes(buf: &mut Bytes) -> Result<Self> {
69 let mut inner = read_length_prefixed_slice(buf)?;
70 let inner = inner.read()?;
71 Ok(LengthPrefixed { inner })
72 }
73}
74
75impl ReadFromBytes for Bytes {
76 fn read_from_bytes(buf: &mut Bytes) -> Result<Self> {
77 Ok(buf.slice(..))
78 }
79}
80
81fn read_length_prefixed_slice(buf: &mut Bytes) -> Result<Bytes> {
82 if buf.remaining() < 4 {
83 bail!(
84 "Remaining buffer too short to contain length of length-prefixed field. Remaining: {}",
85 buf.remaining()
86 );
87 }
88 let len = buf.get_u32_le() as usize;
89 if len > buf.remaining() {
90 bail!(
91 "length-prefixed field longer than remaining buffer. Field length: {}, remaining: {}",
92 len,
93 buf.remaining()
94 );
95 }
96 Ok(buf.split_to(len))
97}