Skip to content

Commit e78b501

Browse files
committed
Provide NonZero_c_* integers
I'm pretty sure I am going want this for #73125 and it seems like an omission that would be in any case good to remedy. It's a shame we don't have competent token pasting and case mangling for use in macro_rules!. Signed-off-by: Ian Jackson <[email protected]>
1 parent d6b9d9a commit e78b501

File tree

2 files changed

+48
-18
lines changed

2 files changed

+48
-18
lines changed

library/std/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@
263263
#![feature(exact_size_is_empty)]
264264
#![feature(exhaustive_patterns)]
265265
#![feature(extend_one)]
266+
#![feature(extended_key_value_attributes)]
266267
#![feature(external_doc)]
267268
#![feature(fn_traits)]
268269
#![feature(format_args_nl)]

library/std/src/os/raw/mod.rs

+47-18
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,41 @@
1111
#[cfg(test)]
1212
mod tests;
1313

14-
macro_rules! type_alias {
15-
{ $Docfile:tt, $Alias:ident = $Real:ty; $( $Cfg:tt )* } => {
14+
use core::num::*;
15+
16+
macro_rules! type_alias_no_nz {
17+
{
18+
$Docfile:tt, $Alias:ident = $Real:ty;
19+
$( $Cfg:tt )*
20+
} => {
1621
#[doc(include = $Docfile)]
1722
$( $Cfg )*
1823
#[stable(feature = "raw_os", since = "1.1.0")]
1924
pub type $Alias = $Real;
2025
}
2126
}
2227

23-
type_alias! { "char.md", c_char = u8;
28+
// To verify that the NonZero types in this file's macro invocations correspond
29+
//
30+
// perl -n < library/std/src/os/raw/mod.rs -e 'next unless m/type_alias\!/; die "$_ ?" unless m/, (c_\w+) = (\w+), NonZero_(\w+) = NonZero(\w+)/; die "$_ ?" unless $3 eq $1 and $4 eq ucfirst $2'
31+
//
32+
// NB this does not check that the main c_* types are right.
33+
34+
macro_rules! type_alias {
35+
{
36+
$Docfile:tt, $Alias:ident = $Real:ty, $NZAlias:ident = $NZReal:ty;
37+
$( $Cfg:tt )*
38+
} => {
39+
type_alias_no_nz! { $Docfile, $Alias = $Real; $( $Cfg )* }
40+
41+
#[doc = concat!("Type alias for `NonZero` version of [`", stringify!($Alias), "`]")]
42+
#[unstable(feature = "raw_os_nonzero", issue = "none")]
43+
$( $Cfg )*
44+
pub type $NZAlias = $NZReal;
45+
}
46+
}
47+
48+
type_alias! { "char.md", c_char = u8, NonZero_c_char = NonZeroU8;
2449
#[cfg(any(
2550
all(
2651
target_os = "linux",
@@ -62,7 +87,7 @@ type_alias! { "char.md", c_char = u8;
6287
),
6388
all(target_os = "fuchsia", target_arch = "aarch64")
6489
))]}
65-
type_alias! { "char.md", c_char = i8;
90+
type_alias! { "char.md", c_char = i8, NonZero_c_char = NonZeroI8;
6691
#[cfg(not(any(
6792
all(
6893
target_os = "linux",
@@ -104,20 +129,24 @@ type_alias! { "char.md", c_char = i8;
104129
),
105130
all(target_os = "fuchsia", target_arch = "aarch64")
106131
)))]}
107-
type_alias! { "schar.md", c_schar = i8; }
108-
type_alias! { "uchar.md", c_uchar = u8; }
109-
type_alias! { "short.md", c_short = i16; }
110-
type_alias! { "ushort.md", c_ushort = u16; }
111-
type_alias! { "int.md", c_int = i32; }
112-
type_alias! { "uint.md", c_uint = u32; }
113-
type_alias! { "long.md", c_long = i32; #[cfg(any(target_pointer_width = "32", windows))] }
114-
type_alias! { "ulong.md", c_ulong = u32; #[cfg(any(target_pointer_width = "32", windows))] }
115-
type_alias! { "long.md", c_long = i64; #[cfg(all(target_pointer_width = "64", not(windows)))] }
116-
type_alias! { "ulong.md", c_ulong = u64; #[cfg(all(target_pointer_width = "64", not(windows)))] }
117-
type_alias! { "longlong.md", c_longlong = i64; }
118-
type_alias! { "ulonglong.md", c_ulonglong = u64; }
119-
type_alias! { "float.md", c_float = f32; }
120-
type_alias! { "double.md", c_double = f64; }
132+
type_alias! { "schar.md", c_schar = i8, NonZero_c_schar = NonZeroI8; }
133+
type_alias! { "uchar.md", c_uchar = u8, NonZero_c_uchar = NonZeroU8; }
134+
type_alias! { "short.md", c_short = i16, NonZero_c_short = NonZeroI16; }
135+
type_alias! { "ushort.md", c_ushort = u16, NonZero_c_ushort = NonZeroU16; }
136+
type_alias! { "int.md", c_int = i32, NonZero_c_int = NonZeroI32; }
137+
type_alias! { "uint.md", c_uint = u32, NonZero_c_uint = NonZeroU32; }
138+
type_alias! { "long.md", c_long = i32, NonZero_c_long = NonZeroI32;
139+
#[cfg(any(target_pointer_width = "32", windows))] }
140+
type_alias! { "ulong.md", c_ulong = u32, NonZero_c_ulong = NonZeroU32;
141+
#[cfg(any(target_pointer_width = "32", windows))] }
142+
type_alias! { "long.md", c_long = i64, NonZero_c_long = NonZeroI64;
143+
#[cfg(all(target_pointer_width = "64", not(windows)))] }
144+
type_alias! { "ulong.md", c_ulong = u64, NonZero_c_ulong = NonZeroU64;
145+
#[cfg(all(target_pointer_width = "64", not(windows)))] }
146+
type_alias! { "longlong.md", c_longlong = i64, NonZero_c_longlong = NonZeroI64; }
147+
type_alias! { "ulonglong.md", c_ulonglong = u64, NonZero_c_ulonglong = NonZeroU64; }
148+
type_alias_no_nz! { "float.md", c_float = f32; }
149+
type_alias_no_nz! { "double.md", c_double = f64; }
121150

122151
#[stable(feature = "raw_os", since = "1.1.0")]
123152
#[doc(no_inline)]

0 commit comments

Comments
 (0)