extended time writer trait #58
@ -8,6 +8,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
|
||||
|
||||
# [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-beta.4] 2024-01-23
|
||||
|
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "spacepackets"
|
||||
version = "0.7.0"
|
||||
version = "0.8.0"
|
||||
edition = "2021"
|
||||
rust-version = "1.61"
|
||||
authors = ["Robin Mueller <muellerr@irs.uni-stuttgart.de>"]
|
||||
|
@ -1201,6 +1201,10 @@ impl TimeWriter for TimeProvider<DaysLen16Bits> {
|
||||
}
|
||||
Ok(self.len_as_bytes())
|
||||
}
|
||||
|
||||
fn len_written(&self) -> usize {
|
||||
self.len_as_bytes()
|
||||
}
|
||||
}
|
||||
|
||||
impl TimeWriter for TimeProvider<DaysLen24Bits> {
|
||||
@ -1221,6 +1225,10 @@ impl TimeWriter for TimeProvider<DaysLen24Bits> {
|
||||
}
|
||||
Ok(self.len_as_bytes())
|
||||
}
|
||||
|
||||
fn len_written(&self) -> usize {
|
||||
self.len_as_bytes()
|
||||
}
|
||||
}
|
||||
|
||||
impl<DaysLenProvider: ProvidesDaysLength> PartialEq for TimeProvider<DaysLenProvider> {
|
||||
@ -2270,4 +2278,22 @@ mod tests {
|
||||
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()]);
|
||||
}
|
||||
}
|
||||
|
@ -587,6 +587,10 @@ impl TimeWriter for TimeProviderCcsdsEpoch {
|
||||
}
|
||||
Ok(current_idx)
|
||||
}
|
||||
|
||||
fn len_written(&self) -> usize {
|
||||
self.len_as_bytes()
|
||||
}
|
||||
}
|
||||
|
||||
impl CcsdsTimeProvider for TimeProviderCcsdsEpoch {
|
||||
@ -1226,4 +1230,13 @@ mod tests {
|
||||
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()]);
|
||||
}
|
||||
}
|
||||
|
@ -189,9 +189,18 @@ pub fn ms_of_day(seconds_since_epoch: f64) -> u32 {
|
||||
}
|
||||
|
||||
pub trait TimeWriter {
|
||||
fn len_written(&self) -> usize;
|
||||
|
||||
/// Generic function to convert write a timestamp into a raw buffer.
|
||||
/// Returns the number of written bytes on success.
|
||||
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 {
|
||||
|
Loading…
Reference in New Issue
Block a user