Packet ID trait implementations #30

Merged
muellerr merged 10 commits from packet-id-trait-impls into main 2023-09-18 18:19:32 +02:00
3 changed files with 63 additions and 3 deletions

View File

@ -8,6 +8,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
# [unreleased] # [unreleased]
## Added
- `PacketId` trait impls: `Ord`, `PartialOrd` and `Hash`
# [v0.7.0-beta.1] 2023-08-28 # [v0.7.0-beta.1] 2023-08-28
- Bump `zerocopy` dependency to v0.7.0 - Bump `zerocopy` dependency to v0.7.0

View File

@ -8,6 +8,11 @@ pipeline {
} }
stages { stages {
stage('Rust Toolchain Info') {
steps {
sh 'rustc --version'
}
}
stage('Clippy') { stage('Clippy') {
steps { steps {
sh 'cargo clippy' sh 'cargo clippy'
@ -15,9 +20,11 @@ pipeline {
} }
stage('Docs') { stage('Docs') {
steps { steps {
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
sh 'cargo +nightly doc --all-features' sh 'cargo +nightly doc --all-features'
} }
} }
}
stage('Rustfmt') { stage('Rustfmt') {
steps { steps {
sh 'cargo fmt --all --check' sh 'cargo fmt --all --check'

View File

@ -62,7 +62,10 @@ extern crate alloc;
extern crate std; extern crate std;
use crate::ecss::CCSDS_HEADER_LEN; use crate::ecss::CCSDS_HEADER_LEN;
use core::fmt::{Debug, Display, Formatter}; use core::{
fmt::{Debug, Display, Formatter},
hash::Hash,
};
use crc::{Crc, CRC_16_IBM_3740}; use crc::{Crc, CRC_16_IBM_3740};
use delegate::delegate; use delegate::delegate;
@ -187,7 +190,7 @@ impl TryFrom<u8> for SequenceFlags {
/// Abstraction for the CCSDS Packet ID, which forms the last thirteen bits /// Abstraction for the CCSDS Packet ID, which forms the last thirteen bits
/// of the first two bytes in the CCSDS primary header. /// of the first two bytes in the CCSDS primary header.
#[derive(Debug, PartialEq, Eq, Copy, Clone)] #[derive(Debug, Eq, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct PacketId { pub struct PacketId {
pub ptype: PacketType, pub ptype: PacketType,
@ -195,6 +198,31 @@ pub struct PacketId {
apid: u16, apid: u16,
} }
impl PartialEq for PacketId {
fn eq(&self, other: &Self) -> bool {
self.raw().eq(&other.raw())
}
}
impl PartialOrd for PacketId {
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
self.raw().partial_cmp(&other.raw())
}
}
impl Ord for PacketId {
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
self.raw().cmp(&other.raw())
}
}
impl Hash for PacketId {
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
let raw = self.raw();
raw.hash(state);
}
}
impl Default for PacketId { impl Default for PacketId {
fn default() -> Self { fn default() -> Self {
PacketId { PacketId {
@ -255,6 +283,7 @@ impl PacketId {
self.apid self.apid
} }
#[inline]
pub fn raw(&self) -> u16 { pub fn raw(&self) -> u16 {
((self.ptype as u16) << 12) | ((self.sec_header_flag as u16) << 11) | self.apid ((self.ptype as u16) << 12) | ((self.sec_header_flag as u16) << 11) | self.apid
} }
@ -721,6 +750,8 @@ pub mod zc {
#[cfg(all(test, feature = "std"))] #[cfg(all(test, feature = "std"))]
mod tests { mod tests {
use std::collections::HashSet;
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
use crate::CcsdsPrimaryHeader; use crate::CcsdsPrimaryHeader;
use crate::{ use crate::{
@ -1034,4 +1065,22 @@ mod tests {
assert_eq!(sp_header.ptype(), PacketType::Tc); assert_eq!(sp_header.ptype(), PacketType::Tc);
assert_eq!(sp_header.data_len(), 0); assert_eq!(sp_header.data_len(), 0);
} }
#[test]
fn packet_id_ord_partial_ord() {
let packet_id_small = PacketId::from(1_u16);
let packet_id_larger = PacketId::from(2_u16);
assert!(packet_id_small < packet_id_larger);
assert!(packet_id_larger > packet_id_small);
assert_eq!(
packet_id_small.cmp(&packet_id_larger),
core::cmp::Ordering::Less
);
}
#[test]
fn packet_id_hashable() {
let mut id_set = HashSet::new();
id_set.insert(PacketId::from(1_u16));
}
} }