PreformattedContext

Struct PreformattedContext 

Source
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 via original_type_id)
  • Preformatted Display output as a String
  • Preformatted Debug output as a String
  • Preferred formatting styles for both Display and Debug

§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

Source

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§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<O> ObjectMarkerFor<Local> for O
where O: 'static,

Source§

impl<O> ObjectMarkerFor<SendSync> for O
where O: 'static + Send + Sync,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> OpaqueAttachment for T
where T: Send + Sync + 'static,