Skip to content

Commit

Permalink
Merge pull request 'Packet ID trait implementations' (#30) from packe…
Browse files Browse the repository at this point in the history
…t-id-trait-impls into main

Reviewed-on: https://egit.irs.uni-stuttgart.de/rust/spacepackets/pulls/30
  • Loading branch information
robamu committed Sep 18, 2023
2 parents e935b38 + be37c15 commit 79d26e1
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

# [unreleased]

## Added

- `PacketId` trait impls: `Ord`, `PartialOrd` and `Hash`

# [v0.7.0-beta.1] 2023-08-28

- Bump `zerocopy` dependency to v0.7.0
Expand Down
9 changes: 8 additions & 1 deletion automation/Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,21 @@ pipeline {
}

stages {
stage('Rust Toolchain Info') {
steps {
sh 'rustc --version'
}
}
stage('Clippy') {
steps {
sh 'cargo clippy'
}
}
stage('Docs') {
steps {
sh 'cargo +nightly doc --all-features'
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
sh 'cargo +nightly doc --all-features'
}
}
}
stage('Rustfmt') {
Expand Down
53 changes: 51 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ extern crate alloc;
extern crate std;

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 delegate::delegate;

Expand Down Expand Up @@ -187,14 +190,39 @@ impl TryFrom<u8> for SequenceFlags {

/// Abstraction for the CCSDS Packet ID, which forms the last thirteen bits
/// 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))]
pub struct PacketId {
pub ptype: PacketType,
pub sec_header_flag: bool,
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 {
fn default() -> Self {
PacketId {
Expand Down Expand Up @@ -255,6 +283,7 @@ impl PacketId {
self.apid
}

#[inline]
pub fn raw(&self) -> u16 {
((self.ptype as u16) << 12) | ((self.sec_header_flag as u16) << 11) | self.apid
}
Expand Down Expand Up @@ -721,6 +750,8 @@ pub mod zc {

#[cfg(all(test, feature = "std"))]
mod tests {
use std::collections::HashSet;

#[cfg(feature = "serde")]
use crate::CcsdsPrimaryHeader;
use crate::{
Expand Down Expand Up @@ -1034,4 +1065,22 @@ mod tests {
assert_eq!(sp_header.ptype(), PacketType::Tc);
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));
}
}

0 comments on commit 79d26e1

Please sign in to comment.