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),
}
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
/// re-trying the operation at a later point.
#[derive(Debug, Clone)]
@ -556,12 +562,9 @@ impl VerificationReporter {
.unwrap();
idx += step.byte_width() as usize;
}
params
.failure_code
.to_bytes(
&mut self.source_data_buf[idx..idx + params.failure_code.byte_width() as usize],
)
.map_err(|e| VerificationError::<E>::ByteConversionError(e))?;
params.failure_code.to_bytes(
&mut self.source_data_buf[idx..idx + params.failure_code.byte_width() as usize],
)?;
idx += params.failure_code.byte_width() as usize;
if let Some(failure_data) = params.failure_data {
self.source_data_buf[idx..idx + failure_data.len()].copy_from_slice(failure_data);
@ -732,6 +735,18 @@ mod stdmod {
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 {
fn send(&self, addr: StoreAddr) -> Result<(), StoreAddr>;
}
@ -824,9 +839,7 @@ mod stdmod {
tm: PusTm,
) -> Result<(), VerificationError<StdVerifSenderError>> {
let operation = |mut mg: RwLockWriteGuard<ShareablePoolProvider>| {
let (addr, buf) = mg.free_element(tm.len_packed()).map_err(|e| {
VerificationError::SendError(StdVerifSenderError::StoreError(e))
})?;
let (addr, buf) = mg.free_element(tm.len_packed())?;
tm.write_to(buf).map_err(VerificationError::PusError)?;
drop(mg);
self.tx.send(addr).map_err(|_| {

View File

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