Skip to content

Commit

Permalink
Cleaned up sync tree.
Browse files Browse the repository at this point in the history
  • Loading branch information
Frostie314159 committed Jan 5, 2024
1 parent ede3d8d commit 190b13c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#![forbid(unsafe_code)]
#![feature(
iter_next_chunk,
array_chunks,
slice_as_chunks,
type_alias_impl_trait,
debug_closure_helpers
Expand Down
9 changes: 3 additions & 6 deletions src/tlvs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ pub enum AWDLTLV<'a, MACIterator, LabelIterator, ValueIterator>
where
LabelIterator: IntoIterator<Item = AWDLStr<'a>> + Clone,
<LabelIterator as IntoIterator>::IntoIter: Clone,
MACIterator: IntoIterator<Item = MACAddress>,
<MACIterator as IntoIterator>::IntoIter: Clone,
MACIterator: IntoIterator<Item = MACAddress> + Clone,
ValueIterator: IntoIterator<Item = u8> + Clone,
{
ServiceResponse(ServiceResponseTLV<'a, LabelIterator>),
Expand All @@ -104,8 +103,7 @@ impl<'a, MACIterator, LabelIterator, ValueIterator>
where
LabelIterator: IntoIterator<Item = AWDLStr<'a>> + Clone,
<LabelIterator as IntoIterator>::IntoIter: Clone,
MACIterator: IntoIterator<Item = MACAddress>,
<MACIterator as IntoIterator>::IntoIter: Clone,
MACIterator: IntoIterator<Item = MACAddress> + Clone,
ValueIterator: IntoIterator<Item = u8> + Clone,
{
pub const fn get_type(&self) -> AWDLTLVType {
Expand Down Expand Up @@ -177,8 +175,7 @@ impl<'a, MACIterator, LabelIterator, ValueIterator> TryIntoCtx
where
LabelIterator: IntoIterator<Item = AWDLStr<'a>> + Clone,
<LabelIterator as IntoIterator>::IntoIter: Clone,
MACIterator: IntoIterator<Item = MACAddress> + ExactSizeIterator,
<MACIterator as IntoIterator>::IntoIter: Clone,
MACIterator: IntoIterator<Item = MACAddress> + ExactSizeIterator + Clone,
ValueIterator: IntoIterator<Item = u8> + Clone,
{
type Error = scroll::Error;
Expand Down
35 changes: 22 additions & 13 deletions src/tlvs/sync_elect/sync_tree_tlv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,27 @@ impl Display for ReadMACIterator<'_> {
}
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[derive(Clone, Copy, Debug, Default)]
/// This describes the structure of the AWDL mesh.
/// The contained mac address are in descending order,
/// with the first one being the mesh master and the other ones being sync masters.
pub struct SyncTreeTLV<MACIterator> {
pub struct SyncTreeTLV<I> {
/// The MACs.
pub tree: MACIterator,
pub tree: I,
}
impl<MACIterator> MeasureWith<()> for SyncTreeTLV<MACIterator>
impl<LhsIterator, RhsIterator> PartialEq<SyncTreeTLV<RhsIterator>> for SyncTreeTLV<LhsIterator>
where
MACIterator: IntoIterator<Item = MACAddress> + ExactSizeIterator,
<MACIterator as IntoIterator>::IntoIter: Clone,
LhsIterator: IntoIterator<Item = MACAddress> + Clone,
RhsIterator: IntoIterator<Item = MACAddress> + Clone,
{
fn eq(&self, other: &SyncTreeTLV<RhsIterator>) -> bool {
self.tree.clone().into_iter().eq(other.tree.clone())
}
}
impl<I: IntoIterator<Item = MACAddress> + Clone> Eq for SyncTreeTLV<I> {}
impl<I> MeasureWith<()> for SyncTreeTLV<I>
where
I: IntoIterator<Item = MACAddress> + ExactSizeIterator,
{
fn measure_with(&self, _ctx: &()) -> usize {
self.tree.len() * 6
Expand All @@ -61,10 +70,9 @@ impl<'a> TryFromCtx<'a> for SyncTreeTLV<ReadMACIterator<'a>> {
))
}
}
impl<MACIterator> TryIntoCtx for SyncTreeTLV<MACIterator>
impl<I> TryIntoCtx for SyncTreeTLV<I>
where
MACIterator: IntoIterator<Item = MACAddress>,
<MACIterator as IntoIterator>::IntoIter: Clone,
I: IntoIterator<Item = MACAddress>,
{
type Error = scroll::Error;
fn try_into_ctx(self, buf: &mut [u8], _ctx: ()) -> Result<usize, Self::Error> {
Expand All @@ -79,17 +87,18 @@ where
#[cfg(test)]
#[test]
fn test_sync_tree_tlv() {
use alloc::{vec, vec::Vec};
use alloc::vec;
use mac_parser::ZERO;
use scroll::Pread;

let bytes = &include_bytes!("../../../test_bins/sync_tree_tlv.bin")[3..];

let sync_tree_tlv = bytes.pread::<SyncTreeTLV<_>>(0).unwrap();

assert_eq!(
sync_tree_tlv.tree.collect::<Vec<_>>(),
vec![MACAddress::new([0xbe, 0x70, 0xf3, 0x17, 0x21, 0xf2]), ZERO]
sync_tree_tlv,
SyncTreeTLV {
tree: [MACAddress::new([0xbe, 0x70, 0xf3, 0x17, 0x21, 0xf2]), ZERO]
}
);

let mut buf = vec![0x00; sync_tree_tlv.measure_with(&())];
Expand Down

0 comments on commit 190b13c

Please sign in to comment.