Study a better way to unwrap errors
Something we can have like unwrap, that instead of panic will call vc_terminate_xxxx. Consider this code:
let init_frame: PhysFrame = match mem_allocate_frames(init_count) {
Some(f) => f,
None => vc_terminate_svsm_enomem(),
};
from something generic resembling:
pub trait CustomUnwrap<T> {
fn unwrap_svsm_enomem(self) -> T;
}
impl CustomUnwrap<T> for Option<T> {
fn unwrap_svsm_enomem(self) -> T {
match self {
Some(value) => value,
None => vc_terminate_svsm_enomem(),
}
}
}
https://github.com/AMDESE/linux-svsm/pull/20#issuecomment-1322186169 Moving the discussion here.
@arkivm is it possible to do this in a generic way, for every Option and Result? Something like (not sure this is valid syntax):
Yeah @dubek. Something like this: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=f02235e475a51d042e8649254455c75e
Another more general question is whether we want to directly terminate, or propagate errors up (let's say all the way up to svsm_request_loop()) and there call the relevant vc_terminate_xxxx if needed. Inside the function we'll use a lot of ? operators to detect Err and None etc and return.
Ideally this will allow unit-testing some parts, but not sure it's worth the (big) effort.