Skip to content

Commit

Permalink
Add HeaderVec::from_header_slice(), simplify the From impl
Browse files Browse the repository at this point in the history
This removes the xmacro in favor of a generic From implementation for
`H: Default` and data constructed from `AsRef<[T]>`
  • Loading branch information
cehteh committed Jan 24, 2025
1 parent 20a54c7 commit 09d6827
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 60 deletions.
2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,3 @@ default = ["atomic_append"]
atomic_append = []
std = []

[dependencies]
xmacro = "0.1.2"
79 changes: 21 additions & 58 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@
extern crate alloc;

use core::{
convert::{AsRef, From},
fmt::Debug,
mem::{self, ManuallyDrop, MaybeUninit},
ops::{Deref, DerefMut, Index, IndexMut},
convert::{From, AsRef},
ptr,
ptr::NonNull,
slice::SliceIndex,
};

#[cfg(feature = "std")]
use std::{
borrow::Cow
};
use std::{};

Check warning on line 16 in src/lib.rs

View workflow job for this annotation

GitHub Actions / tests

unused import: `std::{}`

Check warning on line 16 in src/lib.rs

View workflow job for this annotation

GitHub Actions / benches

unused import: `std::{}`

mod weak;
pub use weak::HeaderVecWeak;
Expand Down Expand Up @@ -539,6 +537,14 @@ impl<H, T> HeaderVec<H, T> {
}

impl<H, T: Clone> HeaderVec<H, T> {
/// Creates a new `HeaderVec` with the given header from some data.
pub fn from_header_slice(header: H, slice: impl AsRef<[T]>) -> Self {
let slice = slice.as_ref();
let mut hv = Self::with_capacity(slice.len(), header);
hv.extend_from_slice_intern(slice, None);
hv
}

/// Adds items from a slice to the end of the list.
pub fn extend_from_slice(&mut self, slice: impl AsRef<[T]>) {
self.extend_from_slice_intern(slice.as_ref(), None)
Expand All @@ -547,7 +553,11 @@ impl<H, T: Clone> HeaderVec<H, T> {
/// Adds items from a slice to the end of the list.
/// This method must be used when `HeaderVecWeak` are used. It takes a closure that is responsible for
/// updating the weak references as additional parameter.
pub fn extend_from_slice_with_weakfix(&mut self, slice: impl AsRef<[T]>, weak_fixup: WeakFixupFn) {
pub fn extend_from_slice_with_weakfix(
&mut self,
slice: impl AsRef<[T]>,
weak_fixup: WeakFixupFn,
) {
self.extend_from_slice_intern(slice.as_ref(), Some(weak_fixup));
}

Expand Down Expand Up @@ -762,59 +772,12 @@ where
}
}

/// A helper struct for using the `HeaderVec::from(WithHeader(H, T))`
pub struct WithHeader<H,T>(pub H, pub T);

xmacro::xmacro! {
// Generates a lot `impl From` for `HeaderVec<(), T>` and `HeaderVec<H, T>`
// The later variant is initialized from a tuple (H,T).
$[
attr:
from: lt: generics: where:
()(&[T]) () () ()
()(&mut [T]) () () ()
()(&[T; N]) () (const N: usize) ()
()(&mut[T; N]) () (const N: usize) ()
()([T; N]) () (const N: usize) ()
(#[cfg(feature = "std")])
(Cow<'a, [T]>) ('a,) () (where [T]: ToOwned)
(#[cfg(feature = "std")])
(Box<[T]>) () () ()
(#[cfg(feature = "std")])
(Vec<T>) () () ()
]

$attr
impl<$lt T: Clone, $generics> From<$from> for HeaderVec<(), T> $where {
fn from(from: $from) -> Self {
let mut hv = HeaderVec::new(());
hv.extend_from_slice(from);
hv
}
}

$attr
impl<$lt H, T: Clone, $generics> From<WithHeader<H, $from>> for HeaderVec<H, T> $where {
fn from(from: WithHeader<H, $from>) -> Self {
let mut hv = HeaderVec::new(from.0);
hv.extend_from_slice(from.1);
hv
}
}
}

impl From<&str> for HeaderVec<(), u8> {
fn from(from: &str) -> Self {
let mut hv = HeaderVec::new(());
hv.extend_from_slice(from.as_bytes());
hv
impl<H: Default, T: Clone, U> From<U> for HeaderVec<H, T>
where
U: AsRef<[T]>,
{
fn from(from: U) -> Self {
HeaderVec::from_header_slice(H::default(), from)
}
}

impl<H> From<WithHeader<H, &str>> for HeaderVec<H, u8> {
fn from(from: WithHeader<H, &str>) -> Self {
let mut hv = HeaderVec::new(from.0);
hv.extend_from_slice(from.1.as_bytes());
hv
}
}
13 changes: 13 additions & 0 deletions tests/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,16 @@ fn test_extend_from_slice() {
hv.extend_from_slice(&[3, 4, 5]);
assert_eq!(hv.as_slice(), &[0, 1, 2, 3, 4, 5]);
}

#[test]
fn test_from() {
assert_eq!(HeaderVec::<(), i32>::from(&[1, 2, 3]).as_slice(), [1, 2, 3]);
}

#[test]
fn test_from_str() {
assert_eq!(
HeaderVec::<(), u8>::from("test").as_slice(),
"test".as_bytes()
);
}

0 comments on commit 09d6827

Please sign in to comment.