pub struct PreformattedContext { /* private fields */ }Expand description
A context that has been preformatted into Strings for both
Display and Debug.
This type stores the formatted output of a context along with metadata about
the original type and preferred formatting styles. It’s created
automatically by Report::preformat and should not typically be
constructed manually.
§Stored Information
- The original type’s
TypeId(accessible viaoriginal_type_id) - Preformatted
Displayoutput as aString - Preformatted
Debugoutput as aString - Preferred formatting styles for both
DisplayandDebug
§Examples
use core::any::TypeId;
use rootcause::{preformatted::PreformattedContext, prelude::*};
#[derive(Debug)]
struct MyError;
impl core::fmt::Display for MyError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "my error")
}
}
let original: Report<MyError> = report!(MyError);
let original_type_id = TypeId::of::<MyError>();
let preformatted: Report<PreformattedContext> = original.preformat();
// The preformatted context remembers its original type
assert_eq!(
preformatted.current_context().original_type_id(),
original_type_id
);Implementations§
Source§impl PreformattedContext
impl PreformattedContext
Sourcepub fn original_type_id(&self) -> TypeId
pub fn original_type_id(&self) -> TypeId
Get the TypeId of the original context type before it was
preformatted.
This can be useful for debugging or for implementing custom logic based on the original type, even though the actual type has been erased.
§Examples
use core::any::TypeId;
use rootcause::{preformatted::PreformattedContext, prelude::*};
#[derive(Debug)]
struct DatabaseError {
code: i32,
}
impl core::fmt::Display for DatabaseError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "Database error: {}", self.code)
}
}
let report: Report<DatabaseError> = report!(DatabaseError { code: 404 });
let preformatted: Report<PreformattedContext> = report.preformat();
// Even though the type is now PreformattedContext, we can still check
// what the original type was
assert_eq!(
preformatted.current_context().original_type_id(),
TypeId::of::<DatabaseError>()
);Auto Trait Implementations§
impl Freeze for PreformattedContext
impl RefUnwindSafe for PreformattedContext
impl Send for PreformattedContext
impl Sync for PreformattedContext
impl Unpin for PreformattedContext
impl UnwindSafe for PreformattedContext
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more