Make parse errors implement Send and Sync
diff --git a/src/error.rs b/src/error.rs
index 712b9b7..12be03e 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -15,6 +15,7 @@
};
use buffer::Cursor;
+use thread::ThreadBound;
/// The result of a Syn parser.
pub type Result<T> = std::result::Result<T, Error>;
@@ -26,12 +27,20 @@
/// [module documentation]: index.html
///
/// *This type is available if Syn is built with the `"parsing"` feature.*
-#[derive(Debug, Clone)]
+#[derive(Debug)]
pub struct Error {
- span: Span,
+ // Span is implemented as an index into a thread-local interner to keep the
+ // size small. It is not safe to access from a different thread. We want
+ // errors to be Send and Sync to play nicely with the Failure crate, so pin
+ // the span we're given to its original thread and assume it is
+ // Span::call_site if accessed from any other thread.
+ span: ThreadBound<Span>,
message: String,
}
+#[cfg(test)]
+struct _Test where Error: Send + Sync;
+
impl Error {
/// Usually the [`ParseStream::error`] method will be used instead, which
/// automatically uses the correct span from the current position of the
@@ -70,13 +79,16 @@
/// ```
pub fn new<T: Display>(span: Span, message: T) -> Self {
Error {
- span: span,
+ span: ThreadBound::new(span),
message: message.to_string(),
}
}
pub fn span(&self) -> Span {
- self.span
+ match self.span.get() {
+ Some(span) => *span,
+ None => Span::call_site(),
+ }
}
/// Render the error as an invocation of [`compile_error!`].
@@ -87,23 +99,25 @@
/// [`compile_error!`]: https://doc.rust-lang.org/std/macro.compile_error.html
/// [`parse_macro_input!`]: ../macro.parse_macro_input.html
pub fn to_compile_error(&self) -> TokenStream {
+ let span = self.span();
+
// compile_error!($message)
TokenStream::from_iter(vec![
- TokenTree::Ident(Ident::new("compile_error", self.span)),
+ TokenTree::Ident(Ident::new("compile_error", span)),
TokenTree::Punct({
let mut punct = Punct::new('!', Spacing::Alone);
- punct.set_span(self.span);
+ punct.set_span(span);
punct
}),
TokenTree::Group({
let mut group = Group::new(Delimiter::Brace, {
TokenStream::from_iter(vec![TokenTree::Literal({
let mut string = Literal::string(&self.message);
- string.set_span(self.span);
+ string.set_span(span);
string
})])
});
- group.set_span(self.span);
+ group.set_span(span);
group
}),
])
@@ -124,6 +138,15 @@
}
}
+impl Clone for Error {
+ fn clone(&self) -> Self {
+ Error {
+ span: ThreadBound::new(self.span()),
+ message: self.message.clone(),
+ }
+ }
+}
+
impl std::error::Error for Error {
fn description(&self) -> &str {
"parse error"
diff --git a/src/lib.rs b/src/lib.rs
index a534854..b61a2ad 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -569,6 +569,9 @@
#[cfg(all(any(feature = "full", feature = "derive"), feature = "printing"))]
mod print;
+#[cfg(feature = "parsing")]
+mod thread;
+
////////////////////////////////////////////////////////////////////////////////
#[cfg(any(feature = "parsing", feature = "full", feature = "derive"))]
diff --git a/src/thread.rs b/src/thread.rs
new file mode 100644
index 0000000..2c15912
--- /dev/null
+++ b/src/thread.rs
@@ -0,0 +1,66 @@
+use std::fmt::{self, Debug};
+
+#[cfg(syn_can_use_thread_id)]
+mod imp {
+ use std::thread::{self, ThreadId};
+
+ /// ThreadBound is a Sync-maker and Send-maker that allows accessing a value
+ /// of type T only from the original thread on which the ThreadBound was
+ /// constructed.
+ pub struct ThreadBound<T> {
+ value: T,
+ thread_id: ThreadId,
+ }
+
+ unsafe impl<T> Sync for ThreadBound<T> {}
+
+ // Send bound requires Copy, as otherwise Drop could run in the wrong place.
+ unsafe impl<T: Copy> Send for ThreadBound<T> {}
+
+ impl<T> ThreadBound<T> {
+ pub fn new(value: T) -> Self {
+ ThreadBound {
+ value: value,
+ thread_id: thread::current().id(),
+ }
+ }
+
+ pub fn get(&self) -> Option<&T> {
+ if thread::current().id() == self.thread_id {
+ Some(&self.value)
+ } else {
+ None
+ }
+ }
+ }
+}
+
+#[cfg(not(syn_can_use_thread_id))]
+mod imp {
+ pub struct ThreadBound<T> {
+ value: T,
+ }
+
+ impl<T> ThreadBound<T> {
+ pub fn new(value: T) -> Self {
+ ThreadBound {
+ value: value,
+ }
+ }
+
+ pub fn get(&self) -> Option<&T> {
+ Some(&self.value)
+ }
+ }
+}
+
+pub use self::imp::ThreadBound;
+
+impl<T: Debug> Debug for ThreadBound<T> {
+ fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ match self.get() {
+ Some(value) => Debug::fmt(value, formatter),
+ None => formatter.write_str("unknown"),
+ }
+ }
+}