add some From Impls

This commit is contained in:
Robin Müller 2022-09-10 19:57:58 +02:00
parent 65c423bdd2
commit f87ff7523f
No known key found for this signature in database
GPG Key ID: 9C287E88FED11DF3
2 changed files with 75 additions and 61 deletions

View File

@ -166,6 +166,12 @@ pub enum VerificationError<E> {
PusError(PusError), PusError(PusError),
} }
impl<E> From<ByteConversionError> for VerificationError<E> {
fn from(e: ByteConversionError) -> Self {
VerificationError::ByteConversionError(e)
}
}
/// If a verification operation fails, the passed token will be returned as well. This allows /// If a verification operation fails, the passed token will be returned as well. This allows
/// re-trying the operation at a later point. /// re-trying the operation at a later point.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -556,12 +562,9 @@ impl VerificationReporter {
.unwrap(); .unwrap();
idx += step.byte_width() as usize; idx += step.byte_width() as usize;
} }
params params.failure_code.to_bytes(
.failure_code &mut self.source_data_buf[idx..idx + params.failure_code.byte_width() as usize],
.to_bytes( )?;
&mut self.source_data_buf[idx..idx + params.failure_code.byte_width() as usize],
)
.map_err(|e| VerificationError::<E>::ByteConversionError(e))?;
idx += params.failure_code.byte_width() as usize; idx += params.failure_code.byte_width() as usize;
if let Some(failure_data) = params.failure_data { if let Some(failure_data) = params.failure_data {
self.source_data_buf[idx..idx + failure_data.len()].copy_from_slice(failure_data); self.source_data_buf[idx..idx + failure_data.len()].copy_from_slice(failure_data);
@ -732,6 +735,18 @@ mod stdmod {
RxDisconnected(StoreAddr), RxDisconnected(StoreAddr),
} }
impl From<StoreError> for StdVerifSenderError {
fn from(e: StoreError) -> Self {
StdVerifSenderError::StoreError(e)
}
}
impl From<StoreError> for VerificationError<StdVerifSenderError> {
fn from(e: StoreError) -> Self {
VerificationError::SendError(e.into())
}
}
trait SendBackend: Send { trait SendBackend: Send {
fn send(&self, addr: StoreAddr) -> Result<(), StoreAddr>; fn send(&self, addr: StoreAddr) -> Result<(), StoreAddr>;
} }
@ -824,9 +839,7 @@ mod stdmod {
tm: PusTm, tm: PusTm,
) -> Result<(), VerificationError<StdVerifSenderError>> { ) -> Result<(), VerificationError<StdVerifSenderError>> {
let operation = |mut mg: RwLockWriteGuard<ShareablePoolProvider>| { let operation = |mut mg: RwLockWriteGuard<ShareablePoolProvider>| {
let (addr, buf) = mg.free_element(tm.len_packed()).map_err(|e| { let (addr, buf) = mg.free_element(tm.len_packed())?;
VerificationError::SendError(StdVerifSenderError::StoreError(e))
})?;
tm.write_to(buf).map_err(VerificationError::PusError)?; tm.write_to(buf).map_err(VerificationError::PusError)?;
drop(mg); drop(mg);
self.tx.send(addr).map_err(|_| { self.tx.send(addr).map_err(|_| {

View File

@ -1,54 +1,55 @@
use std::sync::{Arc, Mutex}; // use std::sync::{Arc, Mutex};
//
trait ProvidesFoo { // trait ProvidesFoo {
fn magic(&mut self); // fn magic(&mut self);
} // }
//
struct Foo { // struct Foo {
magic_value: u32 // magic_value: u32,
} // }
//
impl Default for Foo { // impl Default for Foo {
fn default() -> Self { // fn default() -> Self {
Self {magic_value: 42} // Self { magic_value: 42 }
} // }
} // }
//
impl ProvidesFoo for Foo { // impl ProvidesFoo for Foo {
fn magic(&mut self) { // fn magic(&mut self) {
println!("ProvidesFoo magic"); // println!("ProvidesFoo magic {}", self.magic_value);
} // }
} // }
//
pub type SharedFooConcrete = Arc<Mutex<Box<Foo>>>; // type SharedFooConcrete = Arc<Mutex<Box<Foo>>>;
pub type SharedFooTraitObj = Arc<Mutex<Box<dyn ProvidesFoo + Send + Sync>>>; // type SharedFooTraitObj = Arc<Mutex<Box<dyn ProvidesFoo + Send + Sync>>>;
//
struct FooProvider { // #[allow(dead_code)]
foo_as_trait_obj: SharedFooTraitObj // struct FooProvider {
// foo_as_trait_obj: SharedFooTraitObj,
} // }
//
impl FooProvider { // impl FooProvider {
fn magic_and_then_some(&mut self) { // fn magic_and_then_some(&mut self) {
let mut fooguard = self.foo_as_trait_obj.lock().unwrap(); // let mut fooguard = self.foo_as_trait_obj.lock().unwrap();
fooguard.magic(); // fooguard.magic();
println!("Additional magic"); // println!("Additional magic");
} // }
} // }
//
fn uses_shared_foo_boxed_trait_obj(foo: SharedFooTraitObj) { // #[allow(dead_code)]
let mut foo_provider = FooProvider { // fn uses_shared_foo_boxed_trait_obj(foo: SharedFooTraitObj) {
foo_as_trait_obj: foo // let mut foo_provider = FooProvider {
}; // foo_as_trait_obj: foo,
foo_provider.magic_and_then_some(); // };
} // foo_provider.magic_and_then_some();
fn uses_shared_foo_concrete(foo: SharedFooConcrete) { // }
let mut fooguard = foo.lock().unwrap(); // fn uses_shared_foo_concrete(foo: SharedFooConcrete) {
fooguard.magic(); // let mut fooguard = foo.lock().unwrap();
} // fooguard.magic();
// }
//
fn main() { fn main() {
let shared_foo = Arc::new(Mutex::new(Box::new(Foo::default()))); // let shared_foo = Arc::new(Mutex::new(Box::new(Foo::default())));
uses_shared_foo_concrete(shared_foo.clone()); // uses_shared_foo_concrete(shared_foo.clone());
uses_shared_foo_boxed_trait_obj(shared_foo); // // uses_shared_foo_boxed_trait_obj(shared_foo);
} }