Skip to content

Commit 09d6827

Browse files
committed
Add HeaderVec::from_header_slice(), simplify the From impl
This removes the xmacro in favor of a generic From implementation for `H: Default` and data constructed from `AsRef<[T]>`
1 parent 20a54c7 commit 09d6827

File tree

3 files changed

+34
-60
lines changed

3 files changed

+34
-60
lines changed

Diff for: Cargo.toml

-2
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,3 @@ default = ["atomic_append"]
1616
atomic_append = []
1717
std = []
1818

19-
[dependencies]
20-
xmacro = "0.1.2"

Diff for: src/lib.rs

+21-58
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,17 @@
33
extern crate alloc;
44

55
use core::{
6+
convert::{AsRef, From},
67
fmt::Debug,
78
mem::{self, ManuallyDrop, MaybeUninit},
89
ops::{Deref, DerefMut, Index, IndexMut},
9-
convert::{From, AsRef},
1010
ptr,
1111
ptr::NonNull,
1212
slice::SliceIndex,
1313
};
1414

1515
#[cfg(feature = "std")]
16-
use std::{
17-
borrow::Cow
18-
};
16+
use std::{};
1917

2018
mod weak;
2119
pub use weak::HeaderVecWeak;
@@ -539,6 +537,14 @@ impl<H, T> HeaderVec<H, T> {
539537
}
540538

541539
impl<H, T: Clone> HeaderVec<H, T> {
540+
/// Creates a new `HeaderVec` with the given header from some data.
541+
pub fn from_header_slice(header: H, slice: impl AsRef<[T]>) -> Self {
542+
let slice = slice.as_ref();
543+
let mut hv = Self::with_capacity(slice.len(), header);
544+
hv.extend_from_slice_intern(slice, None);
545+
hv
546+
}
547+
542548
/// Adds items from a slice to the end of the list.
543549
pub fn extend_from_slice(&mut self, slice: impl AsRef<[T]>) {
544550
self.extend_from_slice_intern(slice.as_ref(), None)
@@ -547,7 +553,11 @@ impl<H, T: Clone> HeaderVec<H, T> {
547553
/// Adds items from a slice to the end of the list.
548554
/// This method must be used when `HeaderVecWeak` are used. It takes a closure that is responsible for
549555
/// updating the weak references as additional parameter.
550-
pub fn extend_from_slice_with_weakfix(&mut self, slice: impl AsRef<[T]>, weak_fixup: WeakFixupFn) {
556+
pub fn extend_from_slice_with_weakfix(
557+
&mut self,
558+
slice: impl AsRef<[T]>,
559+
weak_fixup: WeakFixupFn,
560+
) {
551561
self.extend_from_slice_intern(slice.as_ref(), Some(weak_fixup));
552562
}
553563

@@ -762,59 +772,12 @@ where
762772
}
763773
}
764774

765-
/// A helper struct for using the `HeaderVec::from(WithHeader(H, T))`
766-
pub struct WithHeader<H,T>(pub H, pub T);
767-
768-
xmacro::xmacro! {
769-
// Generates a lot `impl From` for `HeaderVec<(), T>` and `HeaderVec<H, T>`
770-
// The later variant is initialized from a tuple (H,T).
771-
$[
772-
attr:
773-
from: lt: generics: where:
774-
()(&[T]) () () ()
775-
()(&mut [T]) () () ()
776-
()(&[T; N]) () (const N: usize) ()
777-
()(&mut[T; N]) () (const N: usize) ()
778-
()([T; N]) () (const N: usize) ()
779-
(#[cfg(feature = "std")])
780-
(Cow<'a, [T]>) ('a,) () (where [T]: ToOwned)
781-
(#[cfg(feature = "std")])
782-
(Box<[T]>) () () ()
783-
(#[cfg(feature = "std")])
784-
(Vec<T>) () () ()
785-
]
786-
787-
$attr
788-
impl<$lt T: Clone, $generics> From<$from> for HeaderVec<(), T> $where {
789-
fn from(from: $from) -> Self {
790-
let mut hv = HeaderVec::new(());
791-
hv.extend_from_slice(from);
792-
hv
793-
}
794-
}
795-
796-
$attr
797-
impl<$lt H, T: Clone, $generics> From<WithHeader<H, $from>> for HeaderVec<H, T> $where {
798-
fn from(from: WithHeader<H, $from>) -> Self {
799-
let mut hv = HeaderVec::new(from.0);
800-
hv.extend_from_slice(from.1);
801-
hv
802-
}
803-
}
804-
}
805-
806-
impl From<&str> for HeaderVec<(), u8> {
807-
fn from(from: &str) -> Self {
808-
let mut hv = HeaderVec::new(());
809-
hv.extend_from_slice(from.as_bytes());
810-
hv
775+
impl<H: Default, T: Clone, U> From<U> for HeaderVec<H, T>
776+
where
777+
U: AsRef<[T]>,
778+
{
779+
fn from(from: U) -> Self {
780+
HeaderVec::from_header_slice(H::default(), from)
811781
}
812782
}
813783

814-
impl<H> From<WithHeader<H, &str>> for HeaderVec<H, u8> {
815-
fn from(from: WithHeader<H, &str>) -> Self {
816-
let mut hv = HeaderVec::new(from.0);
817-
hv.extend_from_slice(from.1.as_bytes());
818-
hv
819-
}
820-
}

Diff for: tests/simple.rs

+13
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,16 @@ fn test_extend_from_slice() {
8787
hv.extend_from_slice(&[3, 4, 5]);
8888
assert_eq!(hv.as_slice(), &[0, 1, 2, 3, 4, 5]);
8989
}
90+
91+
#[test]
92+
fn test_from() {
93+
assert_eq!(HeaderVec::<(), i32>::from(&[1, 2, 3]).as_slice(), [1, 2, 3]);
94+
}
95+
96+
#[test]
97+
fn test_from_str() {
98+
assert_eq!(
99+
HeaderVec::<(), u8>::from("test").as_slice(),
100+
"test".as_bytes()
101+
);
102+
}

0 commit comments

Comments
 (0)