Debian Codegraph @

.cargo/vendor/wit-bindgen/src/rt/async_support/error_context.rs

raw
1 //! Raw bindings to `error-context` in the canonical ABI.
2
3 use std::fmt::{self, Debug, Display};
4 use std::ptr;
5 use std::string::String;
6
7 /// Represents the Component Model `error-context` type.
8 #[derive(PartialEq, Eq)]
9 pub struct ErrorContext {
10 handle: u32,
11 }
12
13 impl ErrorContext {
14 /// Call the `error-context.new` canonical built-in function.
15 pub fn new(debug_message: &str) -> ErrorContext {
16 unsafe {
17 let handle = new(debug_message.as_ptr(), debug_message.len());
18 ErrorContext::from_handle(handle)
19 }
20 }
21
22 #[doc(hidden)]
23 pub fn from_handle(handle: u32) -> Self {
24 Self { handle }
25 }
26
27 #[doc(hidden)]
28 pub fn handle(&self) -> u32 {
29 self.handle
30 }
31
32 /// Extract the debug message from a given [`ErrorContext`]
33 pub fn debug_message(&self) -> String {
34 unsafe {
35 let mut ret = RetPtr {
36 ptr: ptr::null_mut(),
37 len: 0,
38 };
39 debug_message(self.handle, &mut ret);
40 String::from_raw_parts(ret.ptr, ret.len, ret.len)
41 }
42 }
43 }
44
45 impl Debug for ErrorContext {
46 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47 f.debug_struct("ErrorContext")
48 .field("debug_message", &self.debug_message())
49 .finish()
50 }
51 }
52
53 impl Display for ErrorContext {
54 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55 Display::fmt(&self.debug_message(), f)
56 }
57 }
58
59 impl std::error::Error for ErrorContext {}
60
61 impl Drop for ErrorContext {
62 fn drop(&mut self) {
63 #[cfg(target_arch = "wasm32")]
64 unsafe {
65 drop(self.handle)
66 }
67 }
68 }
69
70 #[repr(C)]
71 struct RetPtr {
72 ptr: *mut u8,
73 len: usize,
74 }
75
76 #[cfg(not(target_arch = "wasm32"))]
77 unsafe fn new(_: *const u8, _: usize) -> u32 {
78 unreachable!()
79 }
80 #[cfg(not(target_arch = "wasm32"))]
81 fn debug_message(_: u32, _: &mut RetPtr) {
82 unreachable!()
83 }
84
85 #[cfg(target_arch = "wasm32")]
86 #[link(wasm_import_module = "$root")]
87 extern "C" {
88 #[link_name = "[error-context-new-utf8]"]
89 fn new(_: *const u8, _: usize) -> u32;
90 #[link_name = "[error-context-drop]"]
91 fn drop(_: u32);
92 #[link_name = "[error-context-debug-message-utf8]"]
93 fn debug_message(_: u32, _: &mut RetPtr);
94 }