1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

use crate::buffer::{reader::Storage, writer};
use bytes::{Bytes, BytesMut};

/// Concrete chunk of bytes
///
/// This can be returned to allow the caller to defer copying the data until later.
#[derive(Clone, Debug)]
#[must_use = "Chunk should not be discarded"]
pub enum Chunk<'a> {
    Slice(&'a [u8]),
    Bytes(Bytes),
    BytesMut(BytesMut),
}

impl<'a> Default for Chunk<'a> {
    #[inline]
    fn default() -> Self {
        Self::empty()
    }
}

impl<'a> Chunk<'a> {
    #[inline]
    pub fn empty() -> Self {
        Self::Slice(&[])
    }
}

impl<'a> From<&'a [u8]> for Chunk<'a> {
    #[inline]
    fn from(chunk: &'a [u8]) -> Self {
        Self::Slice(chunk)
    }
}

impl<'a> From<Bytes> for Chunk<'a> {
    #[inline]
    fn from(chunk: Bytes) -> Self {
        Self::Bytes(chunk)
    }
}

impl<'a> From<BytesMut> for Chunk<'a> {
    #[inline]
    fn from(chunk: BytesMut) -> Self {
        Self::BytesMut(chunk)
    }
}

impl<'a> core::ops::Deref for Chunk<'a> {
    type Target = [u8];

    #[inline]
    fn deref(&self) -> &Self::Target {
        match self {
            Self::Slice(t) => t,
            Self::Bytes(t) => t,
            Self::BytesMut(t) => t,
        }
    }
}

impl<'a> Storage for Chunk<'a> {
    type Error = core::convert::Infallible;

    #[inline]
    fn buffered_len(&self) -> usize {
        self.len()
    }

    #[inline]
    fn read_chunk(&mut self, watermark: usize) -> Result<Chunk, Self::Error> {
        match self {
            Self::Slice(v) => v.read_chunk(watermark),
            Self::Bytes(v) => v.read_chunk(watermark),
            Self::BytesMut(v) => v.read_chunk(watermark),
        }
    }

    #[inline]
    fn partial_copy_into<Dest>(&mut self, dest: &mut Dest) -> Result<Chunk, Self::Error>
    where
        Dest: writer::Storage + ?Sized,
    {
        match self {
            Self::Slice(v) => v.partial_copy_into(dest),
            Self::Bytes(v) => v.partial_copy_into(dest),
            Self::BytesMut(v) => v.partial_copy_into(dest),
        }
    }

    #[inline]
    fn copy_into<Dest>(&mut self, dest: &mut Dest) -> Result<(), Self::Error>
    where
        Dest: writer::Storage + ?Sized,
    {
        match self {
            Self::Slice(v) => v.copy_into(dest),
            Self::Bytes(v) => v.copy_into(dest),
            Self::BytesMut(v) => v.copy_into(dest),
        }
    }
}