Skip to content

Commit 465f781

Browse files
committed
Restore support for rustc older than 1.89
1 parent ab5231c commit 465f781

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ mod imp;
164164
#[cfg(span_locations)]
165165
mod location;
166166

167+
#[cfg(procmacro2_semver_exempt)]
168+
mod num;
167169
#[cfg(procmacro2_semver_exempt)]
168170
#[allow(dead_code)]
169171
mod rustc_literal_escaper;

src/num.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// TODO: use NonZero<char> in Rust 1.89+
2+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
3+
pub(crate) struct NonZeroChar(char);
4+
5+
impl NonZeroChar {
6+
pub fn new(ch: char) -> Option<Self> {
7+
if ch == '\0' {
8+
None
9+
} else {
10+
Some(NonZeroChar(ch))
11+
}
12+
}
13+
14+
pub fn get(self) -> char {
15+
self.0
16+
}
17+
}

src/rustc_literal_escaper.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//! Utilities for validating (raw) string, char, and byte literals and
55
//! turning escape sequences into the values they represent.
66
7+
use crate::num::NonZeroChar;
78
use std::ffi::CStr;
89
use std::num::NonZero;
910
use std::ops::Range;
@@ -108,7 +109,7 @@ pub fn check_raw_byte_str(src: &str, callback: impl FnMut(Range<usize>, Result<u
108109
/// NOTE: Does no escaping, but produces errors for bare carriage return ('\r').
109110
pub fn check_raw_c_str(
110111
src: &str,
111-
callback: impl FnMut(Range<usize>, Result<NonZero<char>, EscapeError>),
112+
callback: impl FnMut(Range<usize>, Result<NonZeroChar, EscapeError>),
112113
) {
113114
CStr::check_raw(src, callback);
114115
}
@@ -185,11 +186,11 @@ fn char2byte(c: char) -> Result<u8, EscapeError> {
185186
}
186187

187188
impl CheckRaw for CStr {
188-
type RawUnit = NonZero<char>;
189+
type RawUnit = NonZeroChar;
189190

190191
#[inline]
191192
fn char2raw_unit(c: char) -> Result<Self::RawUnit, EscapeError> {
192-
NonZero::new(c).ok_or(EscapeError::NulInCStr)
193+
NonZeroChar::new(c).ok_or(EscapeError::NulInCStr)
193194
}
194195
}
195196

@@ -253,7 +254,7 @@ pub enum MixedUnit {
253254
/// For example, if '¥' appears in a string it is represented here as
254255
/// `MixedUnit::Char('¥')`, and it will be appended to the relevant byte
255256
/// string as the two-byte UTF-8 sequence `[0xc2, 0xa5]`
256-
Char(NonZero<char>),
257+
Char(NonZeroChar),
257258

258259
/// Used for high bytes (`\x80`..`\xff`).
259260
///
@@ -263,9 +264,9 @@ pub enum MixedUnit {
263264
HighByte(NonZero<u8>),
264265
}
265266

266-
impl From<NonZero<char>> for MixedUnit {
267+
impl From<NonZeroChar> for MixedUnit {
267268
#[inline]
268-
fn from(c: NonZero<char>) -> Self {
269+
fn from(c: NonZeroChar) -> Self {
269270
MixedUnit::Char(c)
270271
}
271272
}
@@ -274,7 +275,7 @@ impl From<NonZero<u8>> for MixedUnit {
274275
#[inline]
275276
fn from(byte: NonZero<u8>) -> Self {
276277
if byte.get().is_ascii() {
277-
MixedUnit::Char(NonZero::new(byte.get() as char).unwrap())
278+
MixedUnit::Char(NonZeroChar::new(byte.get() as char).unwrap())
278279
} else {
279280
MixedUnit::HighByte(byte)
280281
}
@@ -286,7 +287,7 @@ impl TryFrom<char> for MixedUnit {
286287

287288
#[inline]
288289
fn try_from(c: char) -> Result<Self, EscapeError> {
289-
NonZero::new(c)
290+
NonZeroChar::new(c)
290291
.map(MixedUnit::Char)
291292
.ok_or(EscapeError::NulInCStr)
292293
}

0 commit comments

Comments
 (0)