MCPcopy Create free account
hub / github.com/bytecodealliance/wasmtime / new

Method new

crates/core/src/error/error.rs:136–201  ·  view source on GitHub ↗
(mut error: E)

Source from the content-addressed store, hash-verified

134impl BoxedDynError {
135 #[inline]
136 fn new<E>(mut error: E) -> Result<Self, OutOfMemory>
137 where
138 // NB: This implies `Send + Sync`, which is necessary for safety.
139 E: ErrorExt,
140 {
141 #[cfg(not(feature = "backtrace"))]
142 let _ = &mut error;
143
144 // Note: do not use `Option::or_else` here to avoid an extra frame
145 // showing up in the backtrace, which would create extra noise for users
146 // to mentally filter out.
147 #[cfg(feature = "backtrace")]
148 let backtrace = match error.take_backtrace() {
149 Some(bt) => bt,
150 None => crate::error::backtrace::capture(),
151 };
152
153 let boxed = try_new_uninit_box()?;
154 let error = Box::write(
155 boxed,
156 ConcreteError {
157 vtable: Vtable::of::<E>(),
158 #[cfg(feature = "backtrace")]
159 backtrace: Some(backtrace),
160 error,
161 },
162 );
163
164 // We are going to pun the `ConcreteError<E>` pointer into a `DynError`
165 // pointer. Debug assert that their layouts are compatible first.
166 #[cfg(debug_assertions)]
167 {
168 let dyn_size = mem::size_of::<DynError>();
169 let concrete_size = mem::size_of::<ConcreteError<E>>();
170 assert!(
171 dyn_size <= concrete_size,
172 "assertion failed: {dyn_size} <= {concrete_size}"
173 );
174
175 let dyn_align = mem::align_of::<DynError>();
176 let concrete_align = mem::align_of::<ConcreteError<E>>();
177 assert!(
178 dyn_align <= concrete_align,
179 "assertion failed: {dyn_align} <= {concrete_align}"
180 );
181
182 let dyn_offset = mem::offset_of!(DynError, vtable);
183 let concrete_offset = mem::offset_of!(ConcreteError<E>, vtable);
184 assert_eq!(dyn_offset, concrete_offset);
185
186 #[cfg(feature = "backtrace")]
187 {
188 let dyn_offset = mem::offset_of!(DynError, backtrace);
189 let concrete_offset = mem::offset_of!(ConcreteError<E>, backtrace);
190 assert_eq!(dyn_offset, concrete_offset);
191 }
192 }
193

Callers

nothing calls this directly

Calls 7

captureFunction · 0.85
try_new_uninit_boxFunction · 0.85
OkFunction · 0.85
ForeignErrorClass · 0.85
writeFunction · 0.50
newFunction · 0.50
take_backtraceMethod · 0.45

Tested by

no test coverage detected