You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For a concrete motivation for this request, I have the following definition:
pub mod api {
#[derive(Error, Debug)]
pub enum Error {
#[error("specified server {0} not found")]
ServerNotFound(String),
...
#[error("expected {0} elements, got {1}")]
UnexpectedData(usize, usize, Box<dyn DebugT>),
#[error("{0}")]
InsufficientData(String, Option<Box<dyn DebugT>>),
}
later, I use it in cases where I want to see the whole thing that lacks the inner thing:
...
let v = self.get_foo().await?;
v.bar.ok_or_else(|| {
app_err::Error::InsufficientData(
"no bar in response".to_string(),
Some(Box::new(v)),
)
})
...
and also in places where there simply isn't a thing at all to see:
...
let v: Option<Vec<Foo>> = outer.pop().unwrap().foo;
let mut v: Vec<Foo> = v.ok_or_else(|| {
app_err::Error::InsufficientData("no Foo vec found".to_string(), None)
})?;
...
Personally, I'd prefer something like #[error(with = fn)], where fn is Fn(..., &mut fmt::Formatter) -> fmt::Result, with ... being references to the fields of the enum variant.
For this sort of code today [#175 (comment)]:
one might instead write something like this, without any
String
allocation:or possibly this:
Unclear whether either of these is that much better than:
or, using an adapter around
Fn(&mut fmt::Formatter) -> fmt::Result
to trim the boilerplate:The text was updated successfully, but these errors were encountered: