我写了一个trait来将对象序列化为小端字节的迭代器:
pub trait ToLeBytes: Sized
where
Self::Iter: Iterator<Item = u8>,
{
type Iter;
fn to_le_bytes(&self) -> Self::Iter;
}
字符串
我已经为我需要的原始数据类型和heapless::Vec
实现了它:
#[allow(clippy::cast_possible_truncation)]
#[cfg(feature = "heapless")]
impl<I, const SIZE: usize> ToLeBytes for heapless::Vec<I, SIZE>
where
I: ToLeBytes,
for<'a> <I as ToLeBytes>::Iter: Iterator<Item = u8> + 'a,
{
type Iter = Box<dyn Iterator<Item = u8>>;
fn to_le_bytes(&self) -> Self::Iter {
let mut iterator: Box<dyn Iterator<Item = u8>> = Box::new(empty());
if u8::try_from(SIZE).is_ok() {
iterator = Box::new(<u8 as ToLeBytes>::to_le_bytes(&(self.len() as u8)));
} else if u16::try_from(SIZE).is_ok() {
iterator = Box::new(<u16 as ToLeBytes>::to_le_bytes(&(self.len() as u16)));
} else if u32::try_from(SIZE).is_ok() {
iterator = Box::new(<u32 as ToLeBytes>::to_le_bytes(&(self.len() as u32)));
} else if u64::try_from(SIZE).is_ok() {
iterator = Box::new(<u64 as ToLeBytes>::to_le_bytes(&(self.len() as u64)));
}
for item in self {
iterator = Box::new(iterator.chain(<I as ToLeBytes>::to_le_bytes(item)));
}
iterator
}
}
型
然而,由于这段代码是要在低性能硬件的嵌入式系统上运行的,我想避免堆分配,因此想摆脱Box
es。
当然,在基本rust中链接迭代器是不可能的,因为每次调用.chain()
都会返回一个新类型的迭代器。因此我想也许宏可以做到这一点,因为我已经为该trait的derive macro做了类似的事情。
然而,我当然不想为任何可能的I
和SIZE
实现主体,而只是为那些在各自的程序中使用的主体。因此,我需要在单态化代码上运行宏。
我试着google一下怎么做,但是没有找到任何结果。我怎么写一个宏,这个宏是在一个impl
块的单态化代码中传递的?我不想要一个完整的解决方案,但是朝着正确的方向前进。
更新
我想我快到了,感谢Chayim的评论:
use crate::ToLeBytes;
use std::array::IntoIter;
use std::iter::FlatMap;
use std::slice::Iter;
pub struct ContainerIterator<'a, T, const HEADER_SIZE: usize>
where
T: ToLeBytes,
{
size_iterator: IntoIter<u8, HEADER_SIZE>,
items_iterator: FlatMap<Iter<'a, T>, <T as ToLeBytes>::Iter, fn(&T) -> <T as ToLeBytes>::Iter>,
}
impl<'a, T, const HEADER_SIZE: usize> ContainerIterator<'a, T, HEADER_SIZE>
where
T: ToLeBytes,
{
fn from_size_iterator_and_slice(size_iterator: IntoIter<u8, HEADER_SIZE>, items: &[T]) -> Self
where
T: ToLeBytes,
{
Self {
size_iterator,
items_iterator: items
.iter()
.flat_map(|item| <T as ToLeBytes>::to_le_bytes(item)),
}
}
}
impl<'a, T, const HEADER_SIZE: usize> Iterator for ContainerIterator<'a, T, HEADER_SIZE>
where
T: ToLeBytes,
{
type Item = u8;
fn next(&mut self) -> Option<Self::Item> {
if let Some(next_header) = self.size_iterator.next() {
Some(next_header)
} else {
self.items_iterator.next()
}
}
}
pub enum SizedContainerIterator<'a, T>
where
T: ToLeBytes,
{
U8(ContainerIterator<'a, T, 1>),
U16(ContainerIterator<'a, T, 2>),
U32(ContainerIterator<'a, T, 4>),
U64(ContainerIterator<'a, T, 8>),
}
impl<'a, T> SizedContainerIterator<'a, T>
where
T: ToLeBytes,
{
pub fn new(items: &[T], capacity: usize) -> SizedContainerIterator<'a, T>
where
T: ToLeBytes,
{
if u8::try_from(capacity).is_ok() {
SizedContainerIterator::U8(ContainerIterator::from_size_iterator_and_slice(
<u8 as ToLeBytes>::to_le_bytes(&(items.len() as u8)),
items,
))
} else if u16::try_from(capacity).is_ok() {
SizedContainerIterator::U16(ContainerIterator::from_size_iterator_and_slice(
<u16 as ToLeBytes>::to_le_bytes(&(items.len() as u16)),
items,
))
} else if u32::try_from(capacity).is_ok() {
SizedContainerIterator::U32(ContainerIterator::from_size_iterator_and_slice(
<u32 as ToLeBytes>::to_le_bytes(&(items.len() as u32)),
items,
))
} else if u64::try_from(capacity).is_ok() {
SizedContainerIterator::U64(ContainerIterator::from_size_iterator_and_slice(
<u64 as ToLeBytes>::to_le_bytes(&(items.len() as u64)),
items,
))
} else {
unreachable!("vec size exceeds u64");
}
}
}
impl<'a, T> Iterator for SizedContainerIterator<'a, T>
where
T: ToLeBytes,
{
type Item = u8;
fn next(&mut self) -> Option<Self::Item> {
match self {
Self::U8(iterator) => iterator.next(),
Self::U16(iterator) => iterator.next(),
Self::U32(iterator) => iterator.next(),
Self::U64(iterator) => iterator.next(),
}
}
}
型
然而,现在我遇到了为Iter
类型指定生存期的问题:
#[allow(clippy::cast_possible_truncation)]
#[cfg(feature = "heapless")]
impl<I, const SIZE: usize> ToLeBytes for heapless::Vec<I, SIZE>
where
I: ToLeBytes,
for<'a> <I as ToLeBytes>::Iter: Iterator<Item = u8> + 'a,
{
type Iter = SizedContainerIterator<'_, I>;
fn to_le_bytes(&self) -> Self::Iter {
SizedContainerIterator::new(self, SIZE)
}
}
型
1条答案
按热度按时间kmpatx3s1#
感谢Chayim的提示,我让它工作:
个字符