Skip to content

Commit f49881c

Browse files
Merge pull request #47 from triblespace/codex/identify-next-steps-for-project-advancement
Add ByteSource support for Cow<'static, str>
2 parents 868f0b7 + 9f4b7c9 commit f49881c

File tree

4 files changed

+18
-39
lines changed

4 files changed

+18
-39
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Changelog
22

33
## Unreleased
4+
- added `ByteSource` support for `Cow<'static, T>` where `T: AsRef<[u8]>`
45
- added `ByteArea` for staged file writes with `Section::freeze()` to return `Bytes`
56
- `SectionWriter::reserve` now accepts a zerocopy type instead of an alignment constant
67
- `ByteArea` reuses previous pages so allocations align only to the element type

INVENTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
## Desired Functionality
77
- Example demonstrating Python + winnow parsing.
88
- Additional Kani proofs covering `try_unwrap_owner` and weak references.
9+
- `ByteSource` implementation for `VecDeque<u8>` to support ring buffers.
910

1011
## Discovered Issues
1112
- Missing tests for `pop_front` and `pop_back` helpers.

src/sources.rs

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
//!
1111
//! | Feature | Implementations |
1212
//! | ------------ | ---------------------------------------------------------------- |
13-
//! | `zerocopy` | `&'static [T]`, `Box<T>` and `Vec<T>` for `T: IntoBytes + Immutable` |
14-
//! | *(none)* | `&'static [u8]`, `Box<[u8]>`, `Vec<u8>`, `String`, `&'static str` |
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]>` |
1515
//! | `bytes` | `bytes::Bytes` |
1616
//! | `ownedbytes` | `ownedbytes::OwnedBytes` |
1717
//! | `mmap` | `memmap2::Mmap` and `ByteOwner` for `memmap2::MmapRaw` |
@@ -145,41 +145,27 @@ unsafe impl ByteSource for String {
145145
}
146146
}
147147

148-
#[cfg(feature = "zerocopy")]
149-
unsafe impl<T> ByteSource for Cow<'static, [T]>
150-
where
151-
T: IntoBytes + Immutable + Sync + Send + Clone + 'static,
152-
{
153-
type Owner = Self;
154-
155-
fn as_bytes(&self) -> &[u8] {
156-
let slice: &[T] = self.as_ref();
157-
IntoBytes::as_bytes(slice)
158-
}
159-
160-
fn get_owner(self) -> Self::Owner {
161-
self
162-
}
163-
}
164-
165-
#[cfg(not(feature = "zerocopy"))]
166-
unsafe impl ByteSource for Cow<'static, [u8]> {
148+
unsafe impl ByteSource for &'static str {
167149
type Owner = Self;
168150

169151
fn as_bytes(&self) -> &[u8] {
170-
self.as_ref()
152+
(*self).as_bytes()
171153
}
172154

173155
fn get_owner(self) -> Self::Owner {
174156
self
175157
}
176158
}
177159

178-
unsafe impl ByteSource for &'static str {
160+
unsafe impl<T> ByteSource for Cow<'static, T>
161+
where
162+
T: ?Sized + ToOwned + AsRef<[u8]> + Sync + Send + 'static,
163+
T::Owned: Sync + Send + 'static,
164+
{
179165
type Owner = Self;
180166

181167
fn as_bytes(&self) -> &[u8] {
182-
(*self).as_bytes()
168+
self.as_ref().as_ref()
183169
}
184170

185171
fn get_owner(self) -> Self::Owner {

src/tests.rs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -276,31 +276,22 @@ fn test_cow_u8_borrowed_source() {
276276
assert_eq!(bytes_borrowed.as_ref(), borrowed.as_ref());
277277
}
278278

279-
#[cfg(feature = "zerocopy")]
280279
#[test]
281-
fn test_cow_zerocopy_owned_source() {
280+
fn test_cow_str_owned_source() {
282281
use std::borrow::Cow;
283282

284-
let owned: Cow<'static, [u32]> = Cow::Owned(vec![1, 2, 3, 4]);
283+
let owned: Cow<'static, str> = Cow::Owned(String::from("abcd"));
285284
let bytes_owned = Bytes::from_source(owned.clone());
286-
assert_eq!(
287-
bytes_owned.as_ref(),
288-
zerocopy::IntoBytes::as_bytes(owned.as_ref())
289-
);
285+
assert_eq!(bytes_owned.as_ref(), owned.as_bytes());
290286
}
291287

292-
#[cfg(feature = "zerocopy")]
293288
#[test]
294-
fn test_cow_zerocopy_borrowed_source() {
289+
fn test_cow_str_borrowed_source() {
295290
use std::borrow::Cow;
296291

297-
static BORROWED: [u32; 2] = [5, 6];
298-
let borrowed: Cow<'static, [u32]> = Cow::Borrowed(&BORROWED);
292+
let borrowed: Cow<'static, str> = Cow::Borrowed("abcd");
299293
let bytes_borrowed = Bytes::from_source(borrowed.clone());
300-
assert_eq!(
301-
bytes_borrowed.as_ref(),
302-
zerocopy::IntoBytes::as_bytes(borrowed.as_ref())
303-
);
294+
assert_eq!(bytes_borrowed.as_ref(), borrowed.as_bytes());
304295
}
305296

306297
#[cfg(all(feature = "mmap", feature = "zerocopy"))]

0 commit comments

Comments
 (0)