extended time writer trait
All checks were successful
Rust/spacepackets/pipeline/head This commit looks good

This commit is contained in:
Robin Müller 2024-02-05 15:04:29 +01:00
parent 92403738ca
commit d01309cccf
Signed by: muellerr
GPG Key ID: A649FB78196E3849
5 changed files with 55 additions and 1 deletions

View File

@ -8,6 +8,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
# [unreleased] # [unreleased]
# [v0.8.0] 2024-02-05
## Added
- Added `len_written` and `to_vec` methods to the `TimeWriter` trait.
# [v0.7.0] 2024-02-01 # [v0.7.0] 2024-02-01
# [v0.7.0-beta.4] 2024-01-23 # [v0.7.0-beta.4] 2024-01-23

View File

@ -1,6 +1,6 @@
[package] [package]
name = "spacepackets" name = "spacepackets"
version = "0.7.0" version = "0.8.0"
edition = "2021" edition = "2021"
rust-version = "1.61" rust-version = "1.61"
authors = ["Robin Mueller <muellerr@irs.uni-stuttgart.de>"] authors = ["Robin Mueller <muellerr@irs.uni-stuttgart.de>"]

View File

@ -1201,6 +1201,10 @@ impl TimeWriter for TimeProvider<DaysLen16Bits> {
} }
Ok(self.len_as_bytes()) Ok(self.len_as_bytes())
} }
fn len_written(&self) -> usize {
self.len_as_bytes()
}
} }
impl TimeWriter for TimeProvider<DaysLen24Bits> { impl TimeWriter for TimeProvider<DaysLen24Bits> {
@ -1221,6 +1225,10 @@ impl TimeWriter for TimeProvider<DaysLen24Bits> {
} }
Ok(self.len_as_bytes()) Ok(self.len_as_bytes())
} }
fn len_written(&self) -> usize {
self.len_as_bytes()
}
} }
impl<DaysLenProvider: ProvidesDaysLength> PartialEq for TimeProvider<DaysLenProvider> { impl<DaysLenProvider: ProvidesDaysLength> PartialEq for TimeProvider<DaysLenProvider> {
@ -2270,4 +2278,22 @@ mod tests {
assert_eq!(first, second); assert_eq!(first, second);
} }
} }
#[test]
fn test_stamp_to_vec_u16() {
let stamp = TimeProvider::new_with_u16_days(1, 1);
let stamp_vec = stamp.to_vec().unwrap();
let mut buf: [u8; 7] = [0; 7];
stamp.write_to_bytes(&mut buf).unwrap();
assert_eq!(stamp_vec, buf);
}
#[test]
fn test_stamp_to_vec_u24() {
let stamp = TimeProvider::new_with_u24_days(1, 1).unwrap();
let stamp_vec = stamp.to_vec().unwrap();
let mut buf: [u8; 10] = [0; 10];
stamp.write_to_bytes(&mut buf).unwrap();
assert_eq!(stamp_vec, buf[..stamp.len_written()]);
}
} }

View File

@ -587,6 +587,10 @@ impl TimeWriter for TimeProviderCcsdsEpoch {
} }
Ok(current_idx) Ok(current_idx)
} }
fn len_written(&self) -> usize {
self.len_as_bytes()
}
} }
impl CcsdsTimeProvider for TimeProviderCcsdsEpoch { impl CcsdsTimeProvider for TimeProviderCcsdsEpoch {
@ -1226,4 +1230,13 @@ mod tests {
panic!("unexpected error: {}", cuc_error); panic!("unexpected error: {}", cuc_error);
} }
} }
#[test]
fn test_stamp_to_vec() {
let stamp = TimeProviderCcsdsEpoch::new_u16_counter(100);
let stamp_vec = stamp.to_vec().unwrap();
let mut buf: [u8; 16] = [0; 16];
stamp.write_to_bytes(&mut buf).unwrap();
assert_eq!(stamp_vec, buf[..stamp.len_written()]);
}
} }

View File

@ -189,9 +189,18 @@ pub fn ms_of_day(seconds_since_epoch: f64) -> u32 {
} }
pub trait TimeWriter { pub trait TimeWriter {
fn len_written(&self) -> usize;
/// Generic function to convert write a timestamp into a raw buffer. /// Generic function to convert write a timestamp into a raw buffer.
/// Returns the number of written bytes on success. /// Returns the number of written bytes on success.
fn write_to_bytes(&self, bytes: &mut [u8]) -> Result<usize, TimestampError>; fn write_to_bytes(&self, bytes: &mut [u8]) -> Result<usize, TimestampError>;
#[cfg(feature = "alloc")]
fn to_vec(&self) -> Result<alloc::vec::Vec<u8>, TimestampError> {
let mut vec = alloc::vec![0; self.len_written()];
self.write_to_bytes(&mut vec)?;
Ok(vec)
}
} }
pub trait TimeReader { pub trait TimeReader {