Skip to main content

ReportAttachmentMut

Struct ReportAttachmentMut 

Source
pub struct ReportAttachmentMut<'a, Attachment: ?Sized + 'static = Dynamic> { /* private fields */ }
Expand description

A mutable reference to a ReportAttachment, enabling mutation of the underlying attachment.

§Examples

let mut attachment = ReportAttachment::new_sendsync(41);
{
    let mut attachment_mut = attachment.as_mut();
    *attachment_mut.inner_mut() += 1;
}
println!("The answer: {}", attachment.format_inner());

Implementations§

Source§

impl<'a, A: Sized> ReportAttachmentMut<'a, A>

Source

pub fn inner(&self) -> &A

Returns a reference to the attachment data.

§Examples
let mut attachment = ReportAttachment::new_sendsync(41i32);
let attachment_mut = attachment.as_mut();
let data = attachment_mut.inner();
println!("The answer: {}", *data + 1); // => 42
Source

pub fn inner_mut(&mut self) -> &mut A

Returns a mutable reference to the attachment data.

§Examples
let mut attachment = ReportAttachment::new_sendsync(41i32);
{
    let mut attachment_mut = attachment.as_mut();
    let data = attachment_mut.inner_mut();
    *data += 1;
}
assert_eq!(attachment.format_inner().to_string(), "42");
Source

pub fn into_inner_mut(self) -> &'a mut A

Obtain the mutable reference to the inner attachment data.

This method provides direct access to the attachment’s data when the concrete type A is known at compile time. The attachment type must be Sized to use this method.

§Panics

This method will panic if the actual type of the attachment doesn’t match the type A. For a safe alternative that returns Option, use downcast_inner instead.

§Examples
use rootcause::{
    prelude::*,
    report_attachment::{ReportAttachment, ReportAttachmentMut},
};

let mut attachment: ReportAttachment<i32> = ReportAttachment::new(40i32);
{
    let attachment_mut: ReportAttachmentMut<'_, i32> = attachment.as_mut();
    let number: &mut i32 = attachment_mut.into_inner_mut();
    *number += 2;
}

assert_eq!(attachment.as_ref().inner(), &42i32);
Source§

impl<'a, A: ?Sized> ReportAttachmentMut<'a, A>

Source

pub fn into_dynamic(self) -> ReportAttachmentMut<'a, Dynamic>

Changes the attachment type of the ReportAttachmentMut to Dynamic.

Calling this method is equivalent to calling attachment.into(), however this method has been restricted to only change the attachment to Dynamic.

This method can be useful to help with type inference or to improve code readability, as it more clearly communicates intent.

This method does not actually modify the attachment in any way. It only has the effect of “forgetting” that the attachment actually has the type A.

To get back the attachment with a concrete A you can use the method ReportAttachmentMut::downcast_attachment.

Source

pub fn as_ref(&self) -> ReportAttachmentRef<'_, A>

Returns an immutable reference to the attachment.

Source

pub fn into_ref(self) -> ReportAttachmentRef<'a, A>

Consumes the ReportAttachmentMut and returns a ReportAttachmentRef with same lifetime.

Source

pub fn as_mut(&mut self) -> ReportAttachmentMut<'_, A>

Reborrows the ReportAttachmentMut to return a new ReportAttachmentMut with a shorter lifetime.

Source

pub fn inner_type_id(&self) -> TypeId

Returns the TypeId of the inner attachment.

§Examples
use std::any::TypeId;

use rootcause::{
    markers::Dynamic,
    prelude::*,
    report_attachment::{ReportAttachment, ReportAttachmentMut},
};

let attachment: ReportAttachment<&str> = ReportAttachment::new("text data");
let mut attachment: ReportAttachment<Dynamic> = attachment.into_dynamic();
let attachment_mut: ReportAttachmentMut<'_, Dynamic> = attachment.as_mut();
assert_eq!(attachment_mut.inner_type_id(), TypeId::of::<&str>());
Source

pub fn inner_type_name(&self) -> &'static str

Returns the core::any::type_name of the inner attachment.

§Examples
use rootcause::{
    markers::Dynamic,
    prelude::*,
    report_attachment::{ReportAttachment, ReportAttachmentMut},
};

let attachment: ReportAttachment<&str> = ReportAttachment::new("text data");
let mut attachment: ReportAttachment<Dynamic> = attachment.into_dynamic();
let attachment_mut: ReportAttachmentMut<'_, Dynamic> = attachment.as_mut();
assert_eq!(
    attachment_mut.inner_type_name(),
    core::any::type_name::<&str>()
);
Source

pub fn inner_handler_type_id(&self) -> TypeId

Returns the TypeId of the handler used when creating this attachment.

Each attachment is associated with a specific handler (like handlers::Display or handlers::Debug) that determines how it should be formatted when included in a report. This method allows you to inspect which handler is being used.

Source

pub fn format_inner(&self) -> impl Display + Debug

Formats the inner attachment data with formatting hooks applied.

This method formats the attachment using both its handler and any global formatting hooks that have been registered. The hooks allow for custom formatting behaviors such as filtering, transforming, or decorating the output. The returned object implements both Display and Debug traits.

For direct formatting without hooks, use format_inner_unhooked instead.

Source

pub fn format_inner_unhooked(&self) -> impl Display + Debug

Formats the inner attachment data without applying any formatting hooks.

This method provides direct access to the attachment’s formatting capabilities as defined by its handler, bypassing any global formatting hooks that might modify the output. The returned object implements both Display and Debug traits for flexible formatting options.

For formatted output that includes formatting hooks, use format_inner instead.

Source

pub fn preferred_formatting_style( &self, report_formatting_function: FormattingFunction, ) -> AttachmentFormattingStyle

Returns the preferred formatting style for this attachment with formatting hooks applied.

This method determines how the attachment should be formatted when included in a report, taking into account both the attachment’s handler preferences and any global formatting hooks that might modify the behavior.

§Arguments
  • report_formatting_function: Whether the report in which this attachment will be embedded is being formatted using Display formatting or Debug
Source

pub fn preferred_formatting_style_unhooked( &self, report_formatting_function: FormattingFunction, ) -> AttachmentFormattingStyle

Returns the preferred formatting style for this attachment without formatting hooks.

This method determines how the attachment should be formatted based solely on its handler’s preferences, bypassing any global formatting hooks that might modify the behavior. For formatting that includes hooks, use preferred_formatting_style instead.

§Arguments
  • report_formatting_function: Whether the report in which this attachment will be embedded is being formatted using Display formatting or Debug
Source

pub fn preformat(&self) -> ReportAttachment<PreformattedAttachment, SendSync>

Creates a new attachment, with the inner attachment data preformatted.

This can be useful, as the preformatted attachment is a newly allocated object and additionally is Send+Sync.

See PreformattedAttachment for more information.

PreformattedAttachment

Source§

impl<'a> ReportAttachmentMut<'a, Dynamic>

Source

pub fn downcast_attachment<A>(self) -> Result<ReportAttachmentMut<'a, A>, Self>
where A: Sized + 'static,

Attempts to downcast the attachment reference to a different type A.

This method performs a safe type cast, returning Ok if the attachment actually contains data of type A, or Err with the original reference if the types don’t match.

This method is most useful when going from a Dynamic to a concrete A.

§Examples
use rootcause::{
    markers::Dynamic,
    prelude::*,
    report_attachment::{ReportAttachment, ReportAttachmentMut},
};

let mut attachment: ReportAttachment<Dynamic> = ReportAttachment::new(41i32).into_dynamic();
let attachment_mut: ReportAttachmentMut<'_, Dynamic> = attachment.as_mut();

// Try to downcast to an incorrect type
let wrong_ref: Result<ReportAttachmentMut<'_, String>, _> =
    attachment_mut.downcast_attachment();
assert!(wrong_ref.is_err());

let attachment_mut = wrong_ref.unwrap_err();

// Try to downcast to the correct type
let typed_ref: Result<ReportAttachmentMut<'_, i32>, _> = attachment_mut.downcast_attachment();
assert!(typed_ref.is_ok());
Source

pub unsafe fn downcast_attachment_unchecked<A>( self, ) -> ReportAttachmentMut<'a, A>
where A: Sized + 'static,

Performs an unchecked downcast of the attachment reference to type A.

This method bypasses type checking and performs the cast without verifying that the attachment actually contains data of type A. It is the caller’s responsibility to ensure the cast is valid.

§Safety

The caller must ensure:

  1. The inner attachment is actually of type A. This can be verified by calling inner_type_id() first.
§Examples
use rootcause::{
    markers::Dynamic,
    prelude::*,
    report_attachment::{ReportAttachment, ReportAttachmentMut},
};

let attachment: ReportAttachment<&str> = ReportAttachment::new("text data");
let mut attachment: ReportAttachment<Dynamic> = attachment.into_dynamic();
let attachment_mut: ReportAttachmentMut<'_, Dynamic> = attachment.as_mut();

// SAFETY: We know the attachment contains &str data
let typed_mut: ReportAttachmentMut<'_, &str> =
    unsafe { attachment_mut.downcast_attachment_unchecked() };
Source

pub fn downcast_inner<A>(&self) -> Option<&A>
where A: Sized + 'static,

Attempts to downcast the inner attachment data to an immutable reference of type A.

See ReportAttachmentRef::downcast_inner for more info.

Source

pub unsafe fn downcast_inner_unchecked<A>(&self) -> &A
where A: Sized + 'static,

Performs an unchecked downcast of the inner attachment data to a reference of type A.

See ReportAttachmentRef::downcast_inner_unchecked for more info.

§Safety

The caller must ensure:

  1. The inner attachment is actually of type A. This can be verified by calling inner_type_id() first.
Source

pub fn downcast_inner_mut<A>(&mut self) -> Option<&mut A>
where A: Sized + 'static,

Attempts to downcast the inner attachment data to a reference of type A.

This method performs a safe type cast, returning Some with a reference to the data if the attachment actually contains data of type A, or None if the types don’t match. Unlike downcast_attachment, this method returns a direct reference to the data rather than a ReportAttachmentRef.

§Examples

let mut attachment: ReportAttachment<Dynamic> =
    ReportAttachment::new_sendsync(42).into_dynamic();
let mut attachment_mut: ReportAttachmentMut<'_, Dynamic> = attachment.as_mut();

// Try to downcast to the correct type
let data: Option<&mut i32> = attachment_mut.downcast_inner_mut();
assert_eq!(data, Some(&mut 42));

// Try to downcast to an incorrect type
let wrong_data: Option<&mut String> = attachment_mut.downcast_inner_mut();
assert!(wrong_data.is_none());
Source

pub unsafe fn downcast_inner_mut_unchecked<A>(&mut self) -> &mut A
where A: Sized + 'static,

Performs an unchecked downcast of the inner attachment data to a mutable reference of type A.

§Safety

The caller must ensure:

  1. The inner attachment is actually of type A. This can be verified by calling inner_type_id() first.
§Examples

let mut attachment: ReportAttachment<Dynamic> = ReportAttachment::new(41i32).into_dynamic();
let mut attachment_mut: ReportAttachmentMut<'_, Dynamic> = attachment.as_mut();

// SAFETY: We know the attachment contains i32 data
let data: &mut i32 = unsafe { attachment_mut.downcast_inner_mut_unchecked() };
*data += 1;
assert_eq!(*data, 42);

Trait Implementations§

Source§

impl<'a, A: ?Sized> Debug for ReportAttachmentMut<'a, A>

Source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a, A: ?Sized> Display for ReportAttachmentMut<'a, A>

Source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a, A: Sized> From<ReportAttachmentMut<'a, A>> for ReportAttachmentMut<'a, Dynamic>

Source§

fn from(value: ReportAttachmentMut<'a, A>) -> Self

Converts to this type from the input type.
Source§

impl<'a, A: ?Sized> Unpin for ReportAttachmentMut<'a, A>

Auto Trait Implementations§

§

impl<'a, Attachment> Freeze for ReportAttachmentMut<'a, Attachment>
where Attachment: ?Sized,

§

impl<'a, Attachment> RefUnwindSafe for ReportAttachmentMut<'a, Attachment>
where Attachment: RefUnwindSafe + ?Sized,

§

impl<'a, Attachment = Dynamic> !Send for ReportAttachmentMut<'a, Attachment>

§

impl<'a, Attachment = Dynamic> !Sync for ReportAttachmentMut<'a, Attachment>

§

impl<'a, Attachment = Dynamic> !UnwindSafe for ReportAttachmentMut<'a, Attachment>

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<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.