Skip to content

Commit 38d08c8

Browse files
bors[bot]cuviper
andauthored
Merge #57
57: Emit full paths for `Option` and `Result` r=cuviper a=cuviper This drops any assumption about having `Option` and `Result` through the normal prelude. This also fixes #51 hygiene, as weird local types by the same name won't affect the derived code. Co-authored-by: Josh Stone <[email protected]>
2 parents 58dad2c + b0186e5 commit 38d08c8

File tree

2 files changed

+50
-37
lines changed

2 files changed

+50
-37
lines changed

src/lib.rs

+37-37
Original file line numberDiff line numberDiff line change
@@ -257,59 +257,59 @@ pub fn from_primitive(input: TokenStream) -> TokenStream {
257257
quote! {
258258
impl #import::FromPrimitive for #name {
259259
#[inline]
260-
fn from_i64(n: i64) -> Option<Self> {
260+
fn from_i64(n: i64) -> ::core::option::Option<Self> {
261261
<#inner_ty as #import::FromPrimitive>::from_i64(n).map(#name)
262262
}
263263
#[inline]
264-
fn from_u64(n: u64) -> Option<Self> {
264+
fn from_u64(n: u64) -> ::core::option::Option<Self> {
265265
<#inner_ty as #import::FromPrimitive>::from_u64(n).map(#name)
266266
}
267267
#[inline]
268-
fn from_isize(n: isize) -> Option<Self> {
268+
fn from_isize(n: isize) -> ::core::option::Option<Self> {
269269
<#inner_ty as #import::FromPrimitive>::from_isize(n).map(#name)
270270
}
271271
#[inline]
272-
fn from_i8(n: i8) -> Option<Self> {
272+
fn from_i8(n: i8) -> ::core::option::Option<Self> {
273273
<#inner_ty as #import::FromPrimitive>::from_i8(n).map(#name)
274274
}
275275
#[inline]
276-
fn from_i16(n: i16) -> Option<Self> {
276+
fn from_i16(n: i16) -> ::core::option::Option<Self> {
277277
<#inner_ty as #import::FromPrimitive>::from_i16(n).map(#name)
278278
}
279279
#[inline]
280-
fn from_i32(n: i32) -> Option<Self> {
280+
fn from_i32(n: i32) -> ::core::option::Option<Self> {
281281
<#inner_ty as #import::FromPrimitive>::from_i32(n).map(#name)
282282
}
283283
#[inline]
284-
fn from_i128(n: i128) -> Option<Self> {
284+
fn from_i128(n: i128) -> ::core::option::Option<Self> {
285285
<#inner_ty as #import::FromPrimitive>::from_i128(n).map(#name)
286286
}
287287
#[inline]
288-
fn from_usize(n: usize) -> Option<Self> {
288+
fn from_usize(n: usize) -> ::core::option::Option<Self> {
289289
<#inner_ty as #import::FromPrimitive>::from_usize(n).map(#name)
290290
}
291291
#[inline]
292-
fn from_u8(n: u8) -> Option<Self> {
292+
fn from_u8(n: u8) -> ::core::option::Option<Self> {
293293
<#inner_ty as #import::FromPrimitive>::from_u8(n).map(#name)
294294
}
295295
#[inline]
296-
fn from_u16(n: u16) -> Option<Self> {
296+
fn from_u16(n: u16) -> ::core::option::Option<Self> {
297297
<#inner_ty as #import::FromPrimitive>::from_u16(n).map(#name)
298298
}
299299
#[inline]
300-
fn from_u32(n: u32) -> Option<Self> {
300+
fn from_u32(n: u32) -> ::core::option::Option<Self> {
301301
<#inner_ty as #import::FromPrimitive>::from_u32(n).map(#name)
302302
}
303303
#[inline]
304-
fn from_u128(n: u128) -> Option<Self> {
304+
fn from_u128(n: u128) -> ::core::option::Option<Self> {
305305
<#inner_ty as #import::FromPrimitive>::from_u128(n).map(#name)
306306
}
307307
#[inline]
308-
fn from_f32(n: f32) -> Option<Self> {
308+
fn from_f32(n: f32) -> ::core::option::Option<Self> {
309309
<#inner_ty as #import::FromPrimitive>::from_f32(n).map(#name)
310310
}
311311
#[inline]
312-
fn from_f64(n: f64) -> Option<Self> {
312+
fn from_f64(n: f64) -> ::core::option::Option<Self> {
313313
<#inner_ty as #import::FromPrimitive>::from_f64(n).map(#name)
314314
}
315315
}
@@ -339,7 +339,7 @@ pub fn from_primitive(input: TokenStream) -> TokenStream {
339339

340340
quote! {
341341
if #from_i64_var == #name::#ident as i64 {
342-
Some(#name::#ident)
342+
::core::option::Option::Some(#name::#ident)
343343
}
344344
}
345345
})
@@ -355,14 +355,14 @@ pub fn from_primitive(input: TokenStream) -> TokenStream {
355355
impl #import::FromPrimitive for #name {
356356
#[allow(trivial_numeric_casts)]
357357
#[inline]
358-
fn from_i64(#from_i64_var: i64) -> Option<Self> {
358+
fn from_i64(#from_i64_var: i64) -> ::core::option::Option<Self> {
359359
#(#clauses else)* {
360-
None
360+
::core::option::Option::None
361361
}
362362
}
363363

364364
#[inline]
365-
fn from_u64(n: u64) -> Option<Self> {
365+
fn from_u64(n: u64) -> ::core::option::Option<Self> {
366366
Self::from_i64(n as i64)
367367
}
368368
}
@@ -431,59 +431,59 @@ pub fn to_primitive(input: TokenStream) -> TokenStream {
431431
quote! {
432432
impl #import::ToPrimitive for #name {
433433
#[inline]
434-
fn to_i64(&self) -> Option<i64> {
434+
fn to_i64(&self) -> ::core::option::Option<i64> {
435435
<#inner_ty as #import::ToPrimitive>::to_i64(&self.0)
436436
}
437437
#[inline]
438-
fn to_u64(&self) -> Option<u64> {
438+
fn to_u64(&self) -> ::core::option::Option<u64> {
439439
<#inner_ty as #import::ToPrimitive>::to_u64(&self.0)
440440
}
441441
#[inline]
442-
fn to_isize(&self) -> Option<isize> {
442+
fn to_isize(&self) -> ::core::option::Option<isize> {
443443
<#inner_ty as #import::ToPrimitive>::to_isize(&self.0)
444444
}
445445
#[inline]
446-
fn to_i8(&self) -> Option<i8> {
446+
fn to_i8(&self) -> ::core::option::Option<i8> {
447447
<#inner_ty as #import::ToPrimitive>::to_i8(&self.0)
448448
}
449449
#[inline]
450-
fn to_i16(&self) -> Option<i16> {
450+
fn to_i16(&self) -> ::core::option::Option<i16> {
451451
<#inner_ty as #import::ToPrimitive>::to_i16(&self.0)
452452
}
453453
#[inline]
454-
fn to_i32(&self) -> Option<i32> {
454+
fn to_i32(&self) -> ::core::option::Option<i32> {
455455
<#inner_ty as #import::ToPrimitive>::to_i32(&self.0)
456456
}
457457
#[inline]
458-
fn to_i128(&self) -> Option<i128> {
458+
fn to_i128(&self) -> ::core::option::Option<i128> {
459459
<#inner_ty as #import::ToPrimitive>::to_i128(&self.0)
460460
}
461461
#[inline]
462-
fn to_usize(&self) -> Option<usize> {
462+
fn to_usize(&self) -> ::core::option::Option<usize> {
463463
<#inner_ty as #import::ToPrimitive>::to_usize(&self.0)
464464
}
465465
#[inline]
466-
fn to_u8(&self) -> Option<u8> {
466+
fn to_u8(&self) -> ::core::option::Option<u8> {
467467
<#inner_ty as #import::ToPrimitive>::to_u8(&self.0)
468468
}
469469
#[inline]
470-
fn to_u16(&self) -> Option<u16> {
470+
fn to_u16(&self) -> ::core::option::Option<u16> {
471471
<#inner_ty as #import::ToPrimitive>::to_u16(&self.0)
472472
}
473473
#[inline]
474-
fn to_u32(&self) -> Option<u32> {
474+
fn to_u32(&self) -> ::core::option::Option<u32> {
475475
<#inner_ty as #import::ToPrimitive>::to_u32(&self.0)
476476
}
477477
#[inline]
478-
fn to_u128(&self) -> Option<u128> {
478+
fn to_u128(&self) -> ::core::option::Option<u128> {
479479
<#inner_ty as #import::ToPrimitive>::to_u128(&self.0)
480480
}
481481
#[inline]
482-
fn to_f32(&self) -> Option<f32> {
482+
fn to_f32(&self) -> ::core::option::Option<f32> {
483483
<#inner_ty as #import::ToPrimitive>::to_f32(&self.0)
484484
}
485485
#[inline]
486-
fn to_f64(&self) -> Option<f64> {
486+
fn to_f64(&self) -> ::core::option::Option<f64> {
487487
<#inner_ty as #import::ToPrimitive>::to_f64(&self.0)
488488
}
489489
}
@@ -522,7 +522,7 @@ pub fn to_primitive(input: TokenStream) -> TokenStream {
522522
}
523523
} else {
524524
quote! {
525-
Some(match *self {
525+
::core::option::Option::Some(match *self {
526526
#(#variants,)*
527527
})
528528
}
@@ -532,12 +532,12 @@ pub fn to_primitive(input: TokenStream) -> TokenStream {
532532
impl #import::ToPrimitive for #name {
533533
#[inline]
534534
#[allow(trivial_numeric_casts)]
535-
fn to_i64(&self) -> Option<i64> {
535+
fn to_i64(&self) -> ::core::option::Option<i64> {
536536
#match_expr
537537
}
538538

539539
#[inline]
540-
fn to_u64(&self) -> Option<u64> {
540+
fn to_u64(&self) -> ::core::option::Option<u64> {
541541
self.to_i64().map(|x| x as u64)
542542
}
543543
}
@@ -617,7 +617,7 @@ pub fn num_cast(input: TokenStream) -> TokenStream {
617617
let impl_ = quote! {
618618
impl #import::NumCast for #name {
619619
#[inline]
620-
fn from<T: #import::ToPrimitive>(n: T) -> Option<Self> {
620+
fn from<T: #import::ToPrimitive>(n: T) -> ::core::option::Option<Self> {
621621
<#inner_ty as #import::NumCast>::from(n).map(#name)
622622
}
623623
}
@@ -695,7 +695,7 @@ pub fn num(input: TokenStream) -> TokenStream {
695695
impl #import::Num for #name {
696696
type FromStrRadixErr = <#inner_ty as #import::Num>::FromStrRadixErr;
697697
#[inline]
698-
fn from_str_radix(s: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr> {
698+
fn from_str_radix(s: &str, radix: u32) -> ::core::result::Result<Self, Self::FromStrRadixErr> {
699699
<#inner_ty as #import::Num>::from_str_radix(s, radix).map(#name)
700700
}
701701
}

tests/no_implicit_prelude.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#![no_implicit_prelude]
2+
3+
use ::num_derive::*;
4+
5+
#[derive(FromPrimitive, ToPrimitive)]
6+
enum Color {
7+
Red,
8+
Blue,
9+
Green,
10+
}
11+
12+
#[derive(FromPrimitive, ToPrimitive, NumCast, PartialEq, Zero, One, NumOps, Num)]
13+
struct NewI32(i32);

0 commit comments

Comments
 (0)