Skip to content

Commit a914a61

Browse files
committed
Tweak how the library's interface is implemented
The stable/unstable modules still implemented the *very first* iteration of the `proc_macro` API which has long since gone away. This tweaks notably the `Literal` constructors to match what's proposed today, allowing lossless conversions of literals on the stable implementation.
1 parent b2c9462 commit a914a61

File tree

3 files changed

+143
-167
lines changed

3 files changed

+143
-167
lines changed

src/lib.rs

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -455,20 +455,10 @@ pub struct Literal {
455455
_marker: marker::PhantomData<Rc<()>>,
456456
}
457457

458-
macro_rules! suffixed_int_literals {
458+
macro_rules! int_literals {
459459
($($name:ident => $kind:ident,)*) => ($(
460-
#[allow(unused_comparisons)]
461460
pub fn $name(n: $kind) -> Literal {
462-
Literal::_new(n.into())
463-
}
464-
)*)
465-
}
466-
467-
macro_rules! unsuffixed_int_literals {
468-
($($name:ident => $kind:ident,)*) => ($(
469-
#[allow(unused_comparisons)]
470-
pub fn $name(n: $kind) -> Literal {
471-
Literal::_new(imp::Literal::integer(n as i64))
461+
Literal::_new(imp::Literal::$name(n))
472462
}
473463
)*)
474464
}
@@ -481,7 +471,7 @@ impl Literal {
481471
}
482472
}
483473

484-
suffixed_int_literals! {
474+
int_literals! {
485475
u8_suffixed => u8,
486476
u16_suffixed => u16,
487477
u32_suffixed => u32,
@@ -492,9 +482,7 @@ impl Literal {
492482
i32_suffixed => i32,
493483
i64_suffixed => i64,
494484
isize_suffixed => isize,
495-
}
496485

497-
unsuffixed_int_literals! {
498486
u8_unsuffixed => u8,
499487
u16_unsuffixed => u16,
500488
u32_unsuffixed => u32,
@@ -509,30 +497,30 @@ impl Literal {
509497

510498
pub fn f64_unsuffixed(f: f64) -> Literal {
511499
assert!(f.is_finite());
512-
Literal::_new(imp::Literal::float(f))
500+
Literal::_new(imp::Literal::f64_unsuffixed(f))
513501
}
514502

515503
pub fn f64_suffixed(f: f64) -> Literal {
516504
assert!(f.is_finite());
517-
Literal::_new(f.into())
505+
Literal::_new(imp::Literal::f64_suffixed(f))
518506
}
519507

520508
pub fn f32_unsuffixed(f: f32) -> Literal {
521509
assert!(f.is_finite());
522-
Literal::_new(imp::Literal::float(f as f64))
510+
Literal::_new(imp::Literal::f32_unsuffixed(f))
523511
}
524512

525513
pub fn f32_suffixed(f: f32) -> Literal {
526514
assert!(f.is_finite());
527-
Literal::_new(f.into())
515+
Literal::_new(imp::Literal::f32_suffixed(f))
528516
}
529517

530518
pub fn string(string: &str) -> Literal {
531-
Literal::_new(string.into())
519+
Literal::_new(imp::Literal::string(string))
532520
}
533521

534522
pub fn character(ch: char) -> Literal {
535-
Literal::_new(ch.into())
523+
Literal::_new(imp::Literal::character(ch))
536524
}
537525

538526
pub fn byte_string(s: &[u8]) -> Literal {

src/stable.rs

Lines changed: 75 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
#![allow(dead_code)]
1+
#![cfg_attr(not(procmacro2_semver_exempt), allow(dead_code))]
22

3-
use std::ascii;
43
use std::borrow::Borrow;
54
use std::cell::RefCell;
65
#[cfg(procmacro2_semver_exempt)]
@@ -260,7 +259,7 @@ impl FileInfo {
260259
}
261260
}
262261

263-
/// Computes the offsets of each line in the given source string.
262+
/// Computesthe offsets of each line in the given source string.
264263
#[cfg(procmacro2_semver_exempt)]
265264
fn lines_offsets(s: &str) -> Vec<usize> {
266265
let mut lines = vec![0];
@@ -406,7 +405,7 @@ impl Term {
406405
pub fn new(string: &str, span: Span) -> Term {
407406
Term {
408407
intern: SYMBOLS.with(|s| s.borrow_mut().intern(string)),
409-
span,
408+
span: span,
410409
}
411410
}
412411

@@ -477,25 +476,86 @@ pub struct Literal {
477476
span: Span,
478477
}
479478

479+
macro_rules! suffixed_numbers {
480+
($($name:ident => $kind:ident,)*) => ($(
481+
pub fn $name(n: $kind) -> Literal {
482+
Literal::_new(format!(concat!("{}", stringify!($kind)), n))
483+
}
484+
)*)
485+
}
486+
487+
macro_rules! unsuffixed_numbers {
488+
($($name:ident => $kind:ident,)*) => ($(
489+
pub fn $name(n: $kind) -> Literal {
490+
Literal::_new(n.to_string())
491+
}
492+
)*)
493+
}
494+
480495
impl Literal {
481496
fn _new(text: String) -> Literal {
482497
Literal {
483-
text,
498+
text: text,
484499
span: Span::call_site(),
485500
}
486501
}
487502

488-
pub fn byte_char(byte: u8) -> Literal {
489-
match byte {
490-
0 => Literal::_new(format!("b'\\0'")),
491-
b'\"' => Literal::_new(format!("b'\"'")),
492-
n => {
493-
let mut escaped = "b'".to_string();
494-
escaped.extend(ascii::escape_default(n).map(|c| c as char));
495-
escaped.push('\'');
496-
Literal::_new(escaped)
497-
}
503+
suffixed_numbers! {
504+
u8_suffixed => u8,
505+
u16_suffixed => u16,
506+
u32_suffixed => u32,
507+
u64_suffixed => u64,
508+
usize_suffixed => usize,
509+
i8_suffixed => i8,
510+
i16_suffixed => i16,
511+
i32_suffixed => i32,
512+
i64_suffixed => i64,
513+
isize_suffixed => isize,
514+
515+
f32_suffixed => f32,
516+
f64_suffixed => f64,
517+
}
518+
519+
unsuffixed_numbers! {
520+
u8_unsuffixed => u8,
521+
u16_unsuffixed => u16,
522+
u32_unsuffixed => u32,
523+
u64_unsuffixed => u64,
524+
usize_unsuffixed => usize,
525+
i8_unsuffixed => i8,
526+
i16_unsuffixed => i16,
527+
i32_unsuffixed => i32,
528+
i64_unsuffixed => i64,
529+
isize_unsuffixed => isize,
530+
}
531+
532+
pub fn f32_unsuffixed(f: f32) -> Literal {
533+
let mut s = f.to_string();
534+
if !s.contains(".") {
535+
s.push_str(".0");
498536
}
537+
Literal::_new(s)
538+
}
539+
540+
pub fn f64_unsuffixed(f: f64) -> Literal {
541+
let mut s = f.to_string();
542+
if !s.contains(".") {
543+
s.push_str(".0");
544+
}
545+
Literal::_new(s)
546+
}
547+
548+
pub fn string(t: &str) -> Literal {
549+
let mut s = t.chars()
550+
.flat_map(|c| c.escape_default())
551+
.collect::<String>();
552+
s.push('"');
553+
s.insert(0, '"');
554+
Literal::_new(s)
555+
}
556+
557+
pub fn character(t: char) -> Literal {
558+
Literal::_new(format!("'{}'", t.escape_default().collect::<String>()))
499559
}
500560

501561
pub fn byte_string(bytes: &[u8]) -> Literal {
@@ -516,21 +576,6 @@ impl Literal {
516576
Literal::_new(escaped)
517577
}
518578

519-
pub fn float(n: f64) -> Literal {
520-
if !n.is_finite() {
521-
panic!("Invalid float literal {}", n);
522-
}
523-
let mut s = n.to_string();
524-
if !s.contains('.') {
525-
s += ".0";
526-
}
527-
Literal::_new(s)
528-
}
529-
530-
pub fn integer(s: i64) -> Literal {
531-
Literal::_new(s.to_string())
532-
}
533-
534579
pub fn span(&self) -> Span {
535580
self.span
536581
}
@@ -546,54 +591,6 @@ impl fmt::Display for Literal {
546591
}
547592
}
548593

549-
macro_rules! ints {
550-
($($t:ty,)*) => {$(
551-
impl From<$t> for Literal {
552-
fn from(t: $t) -> Literal {
553-
Literal::_new(format!(concat!("{}", stringify!($t)), t))
554-
}
555-
}
556-
)*}
557-
}
558-
559-
ints! {
560-
u8, u16, u32, u64, usize,
561-
i8, i16, i32, i64, isize,
562-
}
563-
564-
macro_rules! floats {
565-
($($t:ty,)*) => {$(
566-
impl From<$t> for Literal {
567-
fn from(t: $t) -> Literal {
568-
assert!(!t.is_nan());
569-
assert!(!t.is_infinite());
570-
Literal::_new(format!(concat!("{}", stringify!($t)), t))
571-
}
572-
}
573-
)*}
574-
}
575-
576-
floats! {
577-
f32, f64,
578-
}
579-
580-
impl<'a> From<&'a str> for Literal {
581-
fn from(t: &'a str) -> Literal {
582-
let mut s = t.chars()
583-
.flat_map(|c| c.escape_default())
584-
.collect::<String>();
585-
s.push('"');
586-
s.insert(0, '"');
587-
Literal::_new(s)
588-
}
589-
}
590-
591-
impl From<char> for Literal {
592-
fn from(t: char) -> Literal {
593-
Literal::_new(format!("'{}'", t.escape_default().collect::<String>()))
594-
}
595-
}
596-
597594
named!(token_stream -> ::TokenStream, map!(
598595
many0!(token_tree),
599596
|trees| ::TokenStream::_new(TokenStream { inner: trees })

0 commit comments

Comments
 (0)