Skip to content

Commit 687c202

Browse files
committed
libc: make structs non_exhaustive by default.
1 parent 1e8377c commit 687c202

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

src/macros.rs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,20 +101,38 @@ macro_rules! prelude {
101101
/// Implement `Clone` and `Copy` for a struct, as well as `Debug`, `Eq`, `Hash`, and
102102
/// `PartialEq` if the `extra_traits` feature is enabled.
103103
///
104+
/// By default, it will mark a struct as `non_exhaustive`. To opt out, use the attribute
105+
/// `#[@not_non_exhaustive]` as the first attribute of the struct.
106+
///
104107
/// Use [`s_no_extra_traits`] for structs where the `extra_traits` feature does not
105108
/// make sense, and for unions.
106109
macro_rules! s {
107110
($(
111+
$(#[@$not_non_exhaustive:ident])*
108112
$(#[$attr:meta])*
109113
pub $t:ident $i:ident { $($field:tt)* }
110114
)*) => ($(
111-
s!(it: $(#[$attr])* pub $t $i { $($field)* });
115+
s!(it: $(#[@$not_non_exhaustive])* $(#[$attr])* pub $t $i { $($field)* });
112116
)*);
113117

114118
(it: $(#[$attr:meta])* pub union $i:ident { $($field:tt)* }) => (
115119
compile_error!("unions cannot derive extra traits, use s_no_extra_traits instead");
116120
);
117121

122+
(it: #[@not_non_exhaustive] $(#[$attr:meta])* pub struct $i:ident { $($field:tt)* }) => (
123+
__item! {
124+
#[repr(C)]
125+
#[cfg_attr(
126+
feature = "extra_traits",
127+
::core::prelude::v1::derive(Debug, Eq, Hash, PartialEq)
128+
)]
129+
#[::core::prelude::v1::derive(::core::clone::Clone, ::core::marker::Copy)]
130+
#[allow(deprecated)]
131+
$(#[$attr])*
132+
pub struct $i { $($field)* }
133+
}
134+
);
135+
118136
(it: $(#[$attr:meta])* pub struct $i:ident { $($field:tt)* }) => (
119137
__item! {
120138
#[repr(C)]
@@ -124,6 +142,7 @@ macro_rules! s {
124142
)]
125143
#[::core::prelude::v1::derive(::core::clone::Clone, ::core::marker::Copy)]
126144
#[allow(deprecated)]
145+
#[non_exhaustive]
127146
$(#[$attr])*
128147
pub struct $i { $($field)* }
129148
}
@@ -133,6 +152,9 @@ macro_rules! s {
133152
/// Implement `Clone` and `Copy` for a tuple struct, as well as `Debug`, `Eq`, `Hash`,
134153
/// and `PartialEq` if the `extra_traits` feature is enabled.
135154
///
155+
/// By default, it will mark a struct as `non_exhaustive`. To opt out, use the attribute
156+
/// `#[@not_non_exhaustive]` as the first attribute of the struct.
157+
///
136158
/// This is the same as [`s`] but works for tuple structs.
137159
macro_rules! s_paren {
138160
($(
@@ -157,10 +179,11 @@ macro_rules! s_paren {
157179
/// Most items will prefer to use [`s`].
158180
macro_rules! s_no_extra_traits {
159181
($(
182+
$(#[@$not_non_exhaustive:ident])*
160183
$(#[$attr:meta])*
161184
pub $t:ident $i:ident { $($field:tt)* }
162185
)*) => ($(
163-
s_no_extra_traits!(it: $(#[$attr])* pub $t $i { $($field)* });
186+
s_no_extra_traits!(it: $(#[@$not_non_exhaustive])* $(#[$attr])* pub $t $i { $($field)* });
164187
)*);
165188

166189
(it: $(#[$attr:meta])* pub union $i:ident { $($field:tt)* }) => (
@@ -179,11 +202,22 @@ macro_rules! s_no_extra_traits {
179202
}
180203
);
181204

205+
(it: #[@not_non_exhaustive] $(#[$attr:meta])* pub struct $i:ident { $($field:tt)* }) => (
206+
__item! {
207+
#[repr(C)]
208+
#[::core::prelude::v1::derive(::core::clone::Clone, ::core::marker::Copy)]
209+
#[cfg_attr(feature = "extra_traits", ::core::prelude::v1::derive(Debug))]
210+
$(#[$attr])*
211+
pub struct $i { $($field)* }
212+
}
213+
);
214+
182215
(it: $(#[$attr:meta])* pub struct $i:ident { $($field:tt)* }) => (
183216
__item! {
184217
#[repr(C)]
185218
#[::core::prelude::v1::derive(::core::clone::Clone, ::core::marker::Copy)]
186219
#[cfg_attr(feature = "extra_traits", ::core::prelude::v1::derive(Debug))]
220+
#[non_exhaustive]
187221
$(#[$attr])*
188222
pub struct $i { $($field)* }
189223
}

src/unix/linux_like/linux/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,6 @@ s! {
798798
}
799799

800800
// linux/openat2.h
801-
#[non_exhaustive]
802801
pub struct open_how {
803802
pub flags: crate::__u64,
804803
pub mode: crate::__u64,
@@ -1329,7 +1328,6 @@ s! {
13291328

13301329
// linux/pidfd.h
13311330

1332-
#[non_exhaustive]
13331331
pub struct pidfd_info {
13341332
pub mask: crate::__u64,
13351333
pub cgroupid: crate::__u64,

src/unix/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ s! {
149149
pub h_addr_list: *mut *mut c_char,
150150
}
151151

152+
#[@not_non_exhaustive]
152153
pub struct iovec {
153154
pub iov_base: *mut c_void,
154155
pub iov_len: size_t,

0 commit comments

Comments
 (0)