Skip to content

Commit 0fd6d9a

Browse files
committed
ADD: a lot impl From's
These are mostly the std Vec compatible From impls. By default this creates `HeaderVec<(), T>` Additionally when one wants to pass a header one can do that by a `WithHeader(H,T)` tuple struct. The later is mostly for convenience. I took my liberty to introduce my xmacro crate here. The macro expands to ~120 lines of code. When this dependency is not wanted it could be replaced with the expanded code.
1 parent 71f42a1 commit 0fd6d9a

2 files changed

Lines changed: 43 additions & 1 deletion

File tree

Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,7 @@ readme = "README.md"
1414
[features]
1515
default = ["atomic_append"]
1616
atomic_append = []
17-
std = []
17+
std = []
18+
19+
[dependencies]
20+
xmacro = "0.1.2"

src/lib.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ use core::{
1111
slice::SliceIndex,
1212
};
1313

14+
#[cfg(feature = "std")]
15+
use std::{
16+
borrow::Cow
17+
};
1418

1519
mod weak;
1620
pub use weak::HeaderVecWeak;
@@ -756,3 +760,38 @@ where
756760
.finish()
757761
}
758762
}
763+
764+
/// A helper struct for using the `HeaderVec::from(WithHeader(H, T))`
765+
pub struct WithHeader<H,T>(pub H, pub T);
766+
767+
xmacro::xmacro! {
768+
// Generates a lot `impl From` for `HeaderVec<(), T>` and `HeaderVec<H, T>`
769+
// The later variant is initialized from a tuple (H,T).
770+
$[
771+
from: lt: generics: where: conv:
772+
(&[T]) () () () ()
773+
(&mut [T]) () () () ()
774+
(&[T; N]) () (const N: usize) () ()
775+
(&mut[T; N]) () (const N: usize) () ()
776+
([T; N]) () (const N: usize) () (.as_ref())
777+
(Cow<'a, [T]>) ('a,) () (where [T]: ToOwned) (.as_ref())
778+
(Box<[T]>) () () () (.as_ref())
779+
(Vec<T>) () () () (.as_ref())
780+
]
781+
782+
impl<$lt T: Clone, $generics> From<$from> for HeaderVec<(), T> $where {
783+
fn from(from: $from) -> Self {
784+
let mut hv = HeaderVec::new(());
785+
hv.extend_from_slice(from $conv);
786+
hv
787+
}
788+
}
789+
790+
impl<$lt H, T: Clone, $generics> From<WithHeader<H,$from>> for HeaderVec<H, T> $where {
791+
fn from(from: WithHeader<H,$from>) -> Self {
792+
let mut hv = HeaderVec::new(from.0);
793+
hv.extend_from_slice(from.1 $conv);
794+
hv
795+
}
796+
}
797+
}

0 commit comments

Comments
 (0)