openssh_sftp_client_lowlevel/
buffer.rs

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
#![forbid(unsafe_code)]

use bytes::BytesMut;

/// Any type that can act as a buffer.
pub trait ToBuffer {
    /// Returned the buffer.
    fn get_buffer(&mut self) -> Buffer<'_>;
}

/// Buffer that can be used to write data into.
#[derive(Debug)]
pub enum Buffer<'a> {
    /// A `Vec<u8>` acts as a buffer.
    Vector(&'a mut Vec<u8>),

    /// A byte slice acts as a buffer.
    Slice(&'a mut [u8]),

    /// A `BytesMut` acts as a buffer.
    Bytes(&'a mut BytesMut),
}

impl ToBuffer for Vec<u8> {
    #[inline(always)]
    fn get_buffer(&mut self) -> Buffer<'_> {
        Buffer::Vector(self)
    }
}

impl ToBuffer for BytesMut {
    #[inline(always)]
    fn get_buffer(&mut self) -> Buffer<'_> {
        Buffer::Bytes(self)
    }
}

impl ToBuffer for Box<[u8]> {
    #[inline(always)]
    fn get_buffer(&mut self) -> Buffer<'_> {
        Buffer::Slice(&mut *self)
    }
}

impl<const LEN: usize> ToBuffer for [u8; LEN] {
    #[inline(always)]
    fn get_buffer(&mut self) -> Buffer<'_> {
        Buffer::Slice(self)
    }
}