Skip to content

Commit f0f303a

Browse files
committed
Prefer core crate in macro expansions
Prefer using core crate over std in macro expansions wherever a symbol has a stable equivalent in core. This makes it easier to eventually support no_std in the future once error_in_core stabilises.
1 parent 435c7c8 commit f0f303a

File tree

2 files changed

+25
-25
lines changed

2 files changed

+25
-25
lines changed

impl/src/attr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -197,14 +197,14 @@ impl ToTokens for Display<'_> {
197197
let fmt = &self.fmt;
198198
let args = &self.args;
199199
tokens.extend(quote! {
200-
std::write!(__formatter, #fmt #args)
200+
::core::write!(__formatter, #fmt #args)
201201
});
202202
}
203203
}
204204

205205
impl ToTokens for Trait {
206206
fn to_tokens(&self, tokens: &mut TokenStream) {
207207
let trait_name = format_ident!("{}", format!("{:?}", self));
208-
tokens.extend(quote!(std::fmt::#trait_name));
208+
tokens.extend(quote!(::core::fmt::#trait_name));
209209
}
210210
}

impl/src/expand.rs

+23-23
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@ fn impl_struct(input: Struct) -> TokenStream {
4545
};
4646
let dyn_error = quote_spanned!(source.span()=> self.#source #asref.as_dyn_error());
4747
Some(quote! {
48-
std::option::Option::Some(#dyn_error)
48+
::core::option::Option::Some(#dyn_error)
4949
})
5050
} else {
5151
None
5252
};
5353
let source_method = source_body.map(|body| {
5454
quote! {
55-
fn source(&self) -> std::option::Option<&(dyn std::error::Error + 'static)> {
55+
fn source(&self) -> ::core::option::Option<&(dyn std::error::Error + 'static)> {
5656
use thiserror::__private::AsDynError;
5757
#body
5858
}
@@ -66,7 +66,7 @@ fn impl_struct(input: Struct) -> TokenStream {
6666
let source = &source_field.member;
6767
let source_provide = if type_is_option(source_field.ty) {
6868
quote_spanned! {source.span()=>
69-
if let std::option::Option::Some(source) = &self.#source {
69+
if let ::core::option::Option::Some(source) = &self.#source {
7070
source.thiserror_provide(#request);
7171
}
7272
}
@@ -79,7 +79,7 @@ fn impl_struct(input: Struct) -> TokenStream {
7979
None
8080
} else if type_is_option(backtrace_field.ty) {
8181
Some(quote! {
82-
if let std::option::Option::Some(backtrace) = &self.#backtrace {
82+
if let ::core::option::Option::Some(backtrace) = &self.#backtrace {
8383
#request.provide_ref::<std::backtrace::Backtrace>(backtrace);
8484
}
8585
})
@@ -95,7 +95,7 @@ fn impl_struct(input: Struct) -> TokenStream {
9595
}
9696
} else if type_is_option(backtrace_field.ty) {
9797
quote! {
98-
if let std::option::Option::Some(backtrace) = &self.#backtrace {
98+
if let ::core::option::Option::Some(backtrace) = &self.#backtrace {
9999
#request.provide_ref::<std::backtrace::Backtrace>(backtrace);
100100
}
101101
}
@@ -116,7 +116,7 @@ fn impl_struct(input: Struct) -> TokenStream {
116116
let only_field = &input.fields[0].member;
117117
display_implied_bounds.insert((0, Trait::Display));
118118
Some(quote! {
119-
std::fmt::Display::fmt(&self.#only_field, __formatter)
119+
::core::fmt::Display::fmt(&self.#only_field, __formatter)
120120
})
121121
} else if let Some(display) = &input.attrs.display {
122122
display_implied_bounds = display.implied_bounds.clone();
@@ -142,9 +142,9 @@ fn impl_struct(input: Struct) -> TokenStream {
142142
let display_where_clause = display_inferred_bounds.augment_where_clause(input.generics);
143143
quote! {
144144
#[allow(unused_qualifications)]
145-
impl #impl_generics std::fmt::Display for #ty #ty_generics #display_where_clause {
145+
impl #impl_generics ::core::fmt::Display for #ty #ty_generics #display_where_clause {
146146
#[allow(clippy::used_underscore_binding)]
147-
fn fmt(&self, __formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
147+
fn fmt(&self, __formatter: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
148148
#body
149149
}
150150
}
@@ -157,7 +157,7 @@ fn impl_struct(input: Struct) -> TokenStream {
157157
let body = from_initializer(from_field, backtrace_field);
158158
quote! {
159159
#[allow(unused_qualifications)]
160-
impl #impl_generics std::convert::From<#from> for #ty #ty_generics #where_clause {
160+
impl #impl_generics ::core::convert::From<#from> for #ty #ty_generics #where_clause {
161161
#[allow(deprecated)]
162162
fn from(source: #from) -> Self {
163163
#ty #body
@@ -217,16 +217,16 @@ fn impl_enum(input: Enum) -> TokenStream {
217217
let varsource = quote!(source);
218218
let dyn_error = quote_spanned!(source.span()=> #varsource #asref.as_dyn_error());
219219
quote! {
220-
#ty::#ident {#source: #varsource, ..} => std::option::Option::Some(#dyn_error),
220+
#ty::#ident {#source: #varsource, ..} => ::core::option::Option::Some(#dyn_error),
221221
}
222222
} else {
223223
quote! {
224-
#ty::#ident {..} => std::option::Option::None,
224+
#ty::#ident {..} => ::core::option::Option::None,
225225
}
226226
}
227227
});
228228
Some(quote! {
229-
fn source(&self) -> std::option::Option<&(dyn std::error::Error + 'static)> {
229+
fn source(&self) -> ::core::option::Option<&(dyn std::error::Error + 'static)> {
230230
use thiserror::__private::AsDynError;
231231
#[allow(deprecated)]
232232
match self {
@@ -251,7 +251,7 @@ fn impl_enum(input: Enum) -> TokenStream {
251251
let varsource = quote!(source);
252252
let source_provide = if type_is_option(source_field.ty) {
253253
quote_spanned! {source.span()=>
254-
if let std::option::Option::Some(source) = #varsource {
254+
if let ::core::option::Option::Some(source) = #varsource {
255255
source.thiserror_provide(#request);
256256
}
257257
}
@@ -262,7 +262,7 @@ fn impl_enum(input: Enum) -> TokenStream {
262262
};
263263
let self_provide = if type_is_option(backtrace_field.ty) {
264264
quote! {
265-
if let std::option::Option::Some(backtrace) = backtrace {
265+
if let ::core::option::Option::Some(backtrace) = backtrace {
266266
#request.provide_ref::<std::backtrace::Backtrace>(backtrace);
267267
}
268268
}
@@ -290,7 +290,7 @@ fn impl_enum(input: Enum) -> TokenStream {
290290
let varsource = quote!(source);
291291
let source_provide = if type_is_option(source_field.ty) {
292292
quote_spanned! {backtrace.span()=>
293-
if let std::option::Option::Some(source) = #varsource {
293+
if let ::core::option::Option::Some(source) = #varsource {
294294
source.thiserror_provide(#request);
295295
}
296296
}
@@ -310,7 +310,7 @@ fn impl_enum(input: Enum) -> TokenStream {
310310
let backtrace = &backtrace_field.member;
311311
let body = if type_is_option(backtrace_field.ty) {
312312
quote! {
313-
if let std::option::Option::Some(backtrace) = backtrace {
313+
if let ::core::option::Option::Some(backtrace) = backtrace {
314314
#request.provide_ref::<std::backtrace::Backtrace>(backtrace);
315315
}
316316
}
@@ -369,7 +369,7 @@ fn impl_enum(input: Enum) -> TokenStream {
369369
Member::Unnamed(index) => format_ident!("_{}", index),
370370
};
371371
display_implied_bounds.insert((0, Trait::Display));
372-
quote!(std::fmt::Display::fmt(#only_field, __formatter))
372+
quote!(::core::fmt::Display::fmt(#only_field, __formatter))
373373
}
374374
};
375375
for (field, bound) in display_implied_bounds {
@@ -388,8 +388,8 @@ fn impl_enum(input: Enum) -> TokenStream {
388388
let display_where_clause = display_inferred_bounds.augment_where_clause(input.generics);
389389
Some(quote! {
390390
#[allow(unused_qualifications)]
391-
impl #impl_generics std::fmt::Display for #ty #ty_generics #display_where_clause {
392-
fn fmt(&self, __formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
391+
impl #impl_generics ::core::fmt::Display for #ty #ty_generics #display_where_clause {
392+
fn fmt(&self, __formatter: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
393393
#use_as_display
394394
#[allow(unused_variables, deprecated, clippy::used_underscore_binding)]
395395
match #void_deref self {
@@ -410,7 +410,7 @@ fn impl_enum(input: Enum) -> TokenStream {
410410
let body = from_initializer(from_field, backtrace_field);
411411
Some(quote! {
412412
#[allow(unused_qualifications)]
413-
impl #impl_generics std::convert::From<#from> for #ty #ty_generics #where_clause {
413+
impl #impl_generics ::core::convert::From<#from> for #ty #ty_generics #where_clause {
414414
#[allow(deprecated)]
415415
fn from(source: #from) -> Self {
416416
#ty::#variant #body
@@ -466,19 +466,19 @@ fn use_as_display(needs_as_display: bool) -> Option<TokenStream> {
466466
fn from_initializer(from_field: &Field, backtrace_field: Option<&Field>) -> TokenStream {
467467
let from_member = &from_field.member;
468468
let some_source = if type_is_option(from_field.ty) {
469-
quote!(std::option::Option::Some(source))
469+
quote!(::core::option::Option::Some(source))
470470
} else {
471471
quote!(source)
472472
};
473473
let backtrace = backtrace_field.map(|backtrace_field| {
474474
let backtrace_member = &backtrace_field.member;
475475
if type_is_option(backtrace_field.ty) {
476476
quote! {
477-
#backtrace_member: std::option::Option::Some(std::backtrace::Backtrace::capture()),
477+
#backtrace_member: ::core::option::Option::Some(::std::backtrace::Backtrace::capture()),
478478
}
479479
} else {
480480
quote! {
481-
#backtrace_member: std::convert::From::from(std::backtrace::Backtrace::capture()),
481+
#backtrace_member: ::core::convert::From::from(::std::backtrace::Backtrace::capture()),
482482
}
483483
}
484484
});

0 commit comments

Comments
 (0)