embedrs_bytes/buf/
into_buf.rs

1use super::{Buf};
2#[allow(unused_imports)]
3use prelude::*;
4
5#[cfg(feature = "std")]
6use std::io;
7
8/// Conversion into a `Buf`
9///
10/// An `IntoBuf` implementation defines how to convert a value into a `Buf`.
11/// This is common for types that represent byte storage of some kind. `IntoBuf`
12/// may be implemented directly for types or on references for those types.
13///
14/// # Examples
15///
16/// ```
17/// use embedrs_bytes::{Buf, IntoBuf, BigEndian};
18///
19/// let bytes = b"\x00\x01hello world";
20/// let mut buf = bytes.into_buf();
21///
22/// assert_eq!(1, buf.get_u16::<BigEndian>());
23///
24/// let mut rest = [0; 11];
25/// buf.copy_to_slice(&mut rest);
26///
27/// assert_eq!(b"hello world", &rest);
28/// ```
29pub trait IntoBuf {
30    /// The `Buf` type that `self` is being converted into
31    type Buf: Buf;
32
33    /// Creates a `Buf` from a value.
34    ///
35    /// # Examples
36    ///
37    /// ```
38    /// use embedrs_bytes::{Buf, IntoBuf, BigEndian};
39    ///
40    /// let bytes = b"\x00\x01hello world";
41    /// let mut buf = bytes.into_buf();
42    ///
43    /// assert_eq!(1, buf.get_u16::<BigEndian>());
44    ///
45    /// let mut rest = [0; 11];
46    /// buf.copy_to_slice(&mut rest);
47    ///
48    /// assert_eq!(b"hello world", &rest);
49    /// ```
50    fn into_buf(self) -> Self::Buf;
51}
52
53impl<T: Buf> IntoBuf for T {
54    type Buf = Self;
55
56    fn into_buf(self) -> Self {
57        self
58    }
59}
60
61#[cfg(feature = "std")]
62impl<'a> IntoBuf for &'a [u8] {
63    type Buf = io::Cursor<&'a [u8]>;
64
65    fn into_buf(self) -> Self::Buf {
66        io::Cursor::new(self)
67    }
68}
69
70#[cfg(feature = "std")]
71impl<'a> IntoBuf for &'a str {
72    type Buf = io::Cursor<&'a [u8]>;
73
74    fn into_buf(self) -> Self::Buf {
75        self.as_bytes().into_buf()
76    }
77}
78
79#[cfg(feature = "std")]
80impl IntoBuf for Vec<u8> {
81    type Buf = io::Cursor<Vec<u8>>;
82
83    fn into_buf(self) -> Self::Buf {
84        io::Cursor::new(self)
85    }
86}
87
88#[cfg(feature = "std")]
89impl<'a> IntoBuf for &'a Vec<u8> {
90    type Buf = io::Cursor<&'a [u8]>;
91
92    fn into_buf(self) -> Self::Buf {
93        io::Cursor::new(&self[..])
94    }
95}
96
97// Kind of annoying... but this impl is required to allow passing `&'static
98// [u8]` where for<'a> &'a T: IntoBuf is required.
99#[cfg(feature = "std")]
100impl<'a> IntoBuf for &'a &'static [u8] {
101    type Buf = io::Cursor<&'static [u8]>;
102
103    fn into_buf(self) -> Self::Buf {
104        io::Cursor::new(self)
105    }
106}
107
108#[cfg(feature = "std")]
109impl<'a> IntoBuf for &'a &'static str {
110    type Buf = io::Cursor<&'static [u8]>;
111
112    fn into_buf(self) -> Self::Buf {
113        self.as_bytes().into_buf()
114    }
115}
116
117#[cfg(feature = "std")]
118impl IntoBuf for String {
119    type Buf = io::Cursor<Vec<u8>>;
120
121    fn into_buf(self) -> Self::Buf {
122        self.into_bytes().into_buf()
123    }
124}
125
126#[cfg(feature = "std")]
127impl<'a> IntoBuf for &'a String {
128    type Buf = io::Cursor<&'a [u8]>;
129
130    fn into_buf(self) -> Self::Buf {
131        self.as_bytes().into_buf()
132    }
133}
134
135impl IntoBuf for u8 {
136    type Buf = Option<[u8; 1]>;
137
138    fn into_buf(self) -> Self::Buf {
139        Some([self])
140    }
141}
142
143impl IntoBuf for i8 {
144    type Buf = Option<[u8; 1]>;
145
146    fn into_buf(self) -> Self::Buf {
147        Some([self as u8; 1])
148    }
149}