1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
mod frame_impl;
mod kind;
use alloc::boxed::Box;
#[cfg(nightly)]
use core::any::{self, Demand, Provider};
use core::{any::TypeId, fmt, panic::Location};
use self::frame_impl::FrameImpl;
pub use self::kind::{AttachmentKind, FrameKind};
pub struct Frame {
frame: Box<dyn FrameImpl>,
location: &'static Location<'static>,
sources: Box<[Frame]>,
}
impl Frame {
#[must_use]
pub const fn location(&self) -> &'static Location<'static> {
self.location
}
#[allow(missing_docs)]
#[must_use]
#[deprecated(since = "0.2.0", note = "use `sources()` instead")]
pub const fn source(&self) -> Option<&Self> {
self.sources().first()
}
#[must_use]
pub const fn sources(&self) -> &[Self] {
&self.sources
}
#[allow(missing_docs)]
#[must_use]
#[deprecated(since = "0.2.0", note = "use `sources_mut()` instead")]
pub fn source_mut(&mut self) -> Option<&mut Self> {
self.sources_mut().first_mut()
}
#[must_use]
pub fn sources_mut(&mut self) -> &mut [Self] {
&mut self.sources
}
#[must_use]
pub fn kind(&self) -> FrameKind<'_> {
self.frame.kind()
}
#[must_use]
#[cfg(nightly)]
pub fn request_ref<T>(&self) -> Option<&T>
where
T: ?Sized + 'static,
{
any::request_ref(self)
}
#[must_use]
#[cfg(nightly)]
pub fn request_value<T>(&self) -> Option<T>
where
T: 'static,
{
any::request_value(self)
}
#[must_use]
pub fn is<T: Send + Sync + 'static>(&self) -> bool {
self.downcast_ref::<T>().is_some()
}
#[must_use]
pub fn downcast_ref<T: Send + Sync + 'static>(&self) -> Option<&T> {
(TypeId::of::<T>() == Self::type_id(self)).then(|| {
unsafe { &*(self.frame.as_ref() as *const dyn FrameImpl).cast::<T>() }
})
}
#[must_use]
pub fn downcast_mut<T: Send + Sync + 'static>(&mut self) -> Option<&mut T> {
(TypeId::of::<T>() == Self::type_id(self)).then(|| {
unsafe { &mut *(self.frame.as_mut() as *mut dyn FrameImpl).cast::<T>() }
})
}
pub(crate) fn type_id(&self) -> TypeId {
FrameImpl::type_id(&*self.frame)
}
}
#[cfg(nightly)]
impl Provider for Frame {
fn provide<'a>(&'a self, demand: &mut Demand<'a>) {
self.frame.provide(demand);
}
}
impl fmt::Debug for Frame {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut debug = fmt.debug_struct("Frame");
debug.field("location", self.location());
match self.kind() {
FrameKind::Context(context) => {
debug.field("context", &context);
}
FrameKind::Attachment(AttachmentKind::Printable(attachment)) => {
debug.field("attachment", &attachment);
}
FrameKind::Attachment(AttachmentKind::Opaque(_)) => {
debug.field("attachment", &"opaque");
}
}
debug.finish()
}
}