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