1mod frame_impl;
2mod kind;
3
4use alloc::boxed::Box;
5#[cfg(nightly)]
6use core::error;
7use core::{any::TypeId, error::Error, fmt};
8
9use self::frame_impl::FrameImpl;
10pub use self::kind::{AttachmentKind, FrameKind};
11
12pub struct Frame {
24 frame: Box<dyn FrameImpl>,
25 sources: Box<[Frame]>,
26}
27
28impl Frame {
29 #[must_use]
35 pub const fn sources(&self) -> &[Self] {
36 &self.sources
37 }
38
39 #[must_use]
45 pub fn sources_mut(&mut self) -> &mut [Self] {
46 &mut self.sources
47 }
48
49 #[must_use]
51 pub fn kind(&self) -> FrameKind<'_> {
52 self.frame.kind()
53 }
54
55 #[must_use]
57 #[cfg(nightly)]
58 pub fn request_ref<T>(&self) -> Option<&T>
59 where
60 T: ?Sized + 'static,
61 {
62 error::request_ref(self.as_error())
63 }
64
65 #[must_use]
67 #[cfg(nightly)]
68 pub fn request_value<T>(&self) -> Option<T>
69 where
70 T: 'static,
71 {
72 error::request_value(self.as_error())
73 }
74
75 #[must_use]
77 pub fn is<T: Send + Sync + 'static>(&self) -> bool {
78 self.frame.as_any().is::<T>()
79 }
80
81 #[must_use]
83 pub fn downcast_ref<T: Send + Sync + 'static>(&self) -> Option<&T> {
84 self.frame.as_any().downcast_ref()
85 }
86
87 #[must_use]
89 pub fn downcast_mut<T: Send + Sync + 'static>(&mut self) -> Option<&mut T> {
90 self.frame.as_any_mut().downcast_mut()
91 }
92
93 #[must_use]
95 pub fn type_id(&self) -> TypeId {
96 self.frame.as_any().type_id()
97 }
98
99 pub(crate) fn as_error(&self) -> &impl Error {
100 &self.frame
101 }
102}
103
104impl fmt::Debug for Frame {
105 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
106 let mut debug = fmt.debug_struct("Frame");
107
108 match self.kind() {
109 FrameKind::Context(context) => {
110 debug.field("context", &context);
111 debug.finish()
112 }
113 FrameKind::Attachment(AttachmentKind::Printable(attachment)) => {
114 debug.field("attachment", &attachment);
115 debug.finish()
116 }
117 FrameKind::Attachment(AttachmentKind::Opaque(_)) => debug.finish_non_exhaustive(),
118 }
119 }
120}