|
10 | 10 | //! |
11 | 11 | //! | Feature | Implementations | |
12 | 12 | //! | ------------ | ---------------------------------------------------------------- | |
13 | | -//! | `zerocopy` | `&'static [T]`, `Box<T>`, `Vec<T>` for `T: IntoBytes + Immutable` | |
14 | | -//! | *(none)* | `&'static [u8]`, `Box<[u8]>`, `Vec<u8>`, `String`, `&'static str`, `Cow<'static, T>` for `T: AsRef<[u8]>` | |
| 13 | +//! | `zerocopy` | `&'static [T]`, `Box<T>`, `Vec<T>`, `VecDeque<T>` for `T: IntoBytes + Immutable` | |
| 14 | +//! | *(none)* | `&'static [u8]`, `Box<[u8]>`, `Vec<u8>`, `VecDeque<u8>`, `String`, `&'static str`, `Cow<'static, T>` for `T: AsRef<[u8]>` | |
15 | 15 | //! | `bytes` | `bytes::Bytes` | |
16 | 16 | //! | `ownedbytes` | `ownedbytes::OwnedBytes` | |
17 | 17 | //! | `mmap` | `memmap2::Mmap` and `ByteOwner` for `memmap2::MmapRaw` | |
|
36 | 36 | //! # let _ = Bytes::from_source(MyData(vec![1, 2, 3])); |
37 | 37 | //! ``` |
38 | 38 |
|
39 | | -use std::borrow::Cow; |
| 39 | +use std::{borrow::Cow, collections::VecDeque}; |
40 | 40 | use zerocopy::Immutable; |
41 | 41 | #[cfg(feature = "zerocopy")] |
42 | 42 | use zerocopy::IntoBytes; |
@@ -133,6 +133,53 @@ unsafe impl ByteSource for Vec<u8> { |
133 | 133 | } |
134 | 134 | } |
135 | 135 |
|
| 136 | +#[cfg(feature = "zerocopy")] |
| 137 | +unsafe impl<T> ByteSource for VecDeque<T> |
| 138 | +where |
| 139 | + T: IntoBytes + Immutable + Sync + Send + 'static, |
| 140 | +{ |
| 141 | + type Owner = Self; |
| 142 | + |
| 143 | + fn as_bytes(&self) -> &[u8] { |
| 144 | + let (front, back) = self.as_slices(); |
| 145 | + assert!( |
| 146 | + back.is_empty(), |
| 147 | + "VecDeque is not contiguous; call make_contiguous before creating Bytes", |
| 148 | + ); |
| 149 | + IntoBytes::as_bytes(front) |
| 150 | + } |
| 151 | + |
| 152 | + fn get_owner(self) -> Self::Owner { |
| 153 | + assert!( |
| 154 | + self.as_slices().1.is_empty(), |
| 155 | + "VecDeque is not contiguous; call make_contiguous before creating Bytes", |
| 156 | + ); |
| 157 | + self |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +#[cfg(not(feature = "zerocopy"))] |
| 162 | +unsafe impl ByteSource for VecDeque<u8> { |
| 163 | + type Owner = Self; |
| 164 | + |
| 165 | + fn as_bytes(&self) -> &[u8] { |
| 166 | + let (front, back) = self.as_slices(); |
| 167 | + assert!( |
| 168 | + back.is_empty(), |
| 169 | + "VecDeque is not contiguous; call make_contiguous before creating Bytes", |
| 170 | + ); |
| 171 | + front |
| 172 | + } |
| 173 | + |
| 174 | + fn get_owner(self) -> Self::Owner { |
| 175 | + assert!( |
| 176 | + self.as_slices().1.is_empty(), |
| 177 | + "VecDeque is not contiguous; call make_contiguous before creating Bytes", |
| 178 | + ); |
| 179 | + self |
| 180 | + } |
| 181 | +} |
| 182 | + |
136 | 183 | unsafe impl ByteSource for String { |
137 | 184 | type Owner = Self; |
138 | 185 |
|
|
0 commit comments