aboutsummaryrefslogtreecommitdiff
path: root/embassy-executor-macros/src/util
diff options
context:
space:
mode:
Diffstat (limited to 'embassy-executor-macros/src/util')
-rw-r--r--embassy-executor-macros/src/util/ctxt.rs72
-rw-r--r--embassy-executor-macros/src/util/mod.rs1
2 files changed, 0 insertions, 73 deletions
diff --git a/embassy-executor-macros/src/util/ctxt.rs b/embassy-executor-macros/src/util/ctxt.rs
deleted file mode 100644
index 9c78cda01..000000000
--- a/embassy-executor-macros/src/util/ctxt.rs
+++ /dev/null
@@ -1,72 +0,0 @@
1// nifty utility borrowed from serde :)
2// https://github.com/serde-rs/serde/blob/master/serde_derive/src/internals/ctxt.rs
3
4use std::cell::RefCell;
5use std::fmt::Display;
6use std::thread;
7
8use proc_macro2::TokenStream;
9use quote::{quote, ToTokens};
10
11/// A type to collect errors together and format them.
12///
13/// Dropping this object will cause a panic. It must be consumed using `check`.
14///
15/// References can be shared since this type uses run-time exclusive mut checking.
16#[derive(Default)]
17pub struct Ctxt {
18 // The contents will be set to `None` during checking. This is so that checking can be
19 // enforced.
20 errors: RefCell<Option<Vec<syn::Error>>>,
21}
22
23impl Ctxt {
24 /// Create a new context object.
25 ///
26 /// This object contains no errors, but will still trigger a panic if it is not `check`ed.
27 pub fn new() -> Self {
28 Ctxt {
29 errors: RefCell::new(Some(Vec::new())),
30 }
31 }
32
33 /// Add an error to the context object with a tokenenizable object.
34 ///
35 /// The object is used for spanning in error messages.
36 pub fn error_spanned_by<A: ToTokens, T: Display>(&self, obj: A, msg: T) {
37 self.errors
38 .borrow_mut()
39 .as_mut()
40 .unwrap()
41 // Curb monomorphization from generating too many identical methods.
42 .push(syn::Error::new_spanned(obj.into_token_stream(), msg));
43 }
44
45 /// Add one of Syn's parse errors.
46 #[allow(unused)]
47 pub fn syn_error(&self, err: syn::Error) {
48 self.errors.borrow_mut().as_mut().unwrap().push(err);
49 }
50
51 /// Consume this object, producing a formatted error string if there are errors.
52 pub fn check(self) -> Result<(), TokenStream> {
53 let errors = self.errors.borrow_mut().take().unwrap();
54 match errors.len() {
55 0 => Ok(()),
56 _ => Err(to_compile_errors(errors)),
57 }
58 }
59}
60
61fn to_compile_errors(errors: Vec<syn::Error>) -> proc_macro2::TokenStream {
62 let compile_errors = errors.iter().map(syn::Error::to_compile_error);
63 quote!(#(#compile_errors)*)
64}
65
66impl Drop for Ctxt {
67 fn drop(&mut self) {
68 if !thread::panicking() && self.errors.borrow().is_some() {
69 panic!("forgot to check for errors");
70 }
71 }
72}
diff --git a/embassy-executor-macros/src/util/mod.rs b/embassy-executor-macros/src/util/mod.rs
deleted file mode 100644
index 28702809e..000000000
--- a/embassy-executor-macros/src/util/mod.rs
+++ /dev/null
@@ -1 +0,0 @@
1pub mod ctxt;