@@ -279,114 +279,46 @@ macro_rules! c_enum {
279279 ( @ty) => { $crate:: prelude:: CEnumRepr } ;
280280}
281281
282- // This is a pretty horrible hack to allow us to conditionally mark some functions as 'const',
283- // without requiring users of this macro to care "libc_const_extern_fn".
284- //
285- // When 'libc_const_extern_fn' is enabled, we emit the captured 'const' keyword in the expanded
286- // function.
287- //
288- // When 'libc_const_extern_fn' is disabled, we always emit a plain 'pub unsafe extern fn'.
289- // Note that the expression matched by the macro is exactly the same - this allows
290- // users of this macro to work whether or not 'libc_const_extern_fn' is enabled
291- //
292- // Unfortunately, we need to duplicate most of this macro between the 'cfg_if' blocks.
293- // This is because 'const unsafe extern fn' won't even parse on older compilers,
294- // so we need to avoid emitting it at all of 'libc_const_extern_fn'.
295- //
296- // Specifically, moving the 'cfg_if' into the macro body will *not* work. Doing so would cause the
297- // '#[cfg(libc_const_extern_fn)]' to be emitted into user code. The 'cfg' gate will not stop Rust
298- // from trying to parse the 'pub const unsafe extern fn', so users would get a compiler error even
299- // when the 'libc_const_extern_fn' feature is disabled.
300-
301- // FIXME(ctest): ctest can't handle `const extern` functions, we should be able to remove this
302- // cfg completely.
303- // FIXME(ctest): ctest can't handle `$(,)?` so we use `$(,)*` which isn't quite correct.
304- cfg_if ! {
305- if #[ cfg( libc_const_extern_fn) ] {
306- /// Define an `unsafe` function that is const as long as `libc_const_extern_fn` is enabled.
307- macro_rules! f {
308- ( $(
309- $( #[ $attr: meta] ) *
310- pub $( { $constness: ident} ) * fn $i: ident( $( $arg: ident: $argty: ty) , * $( , ) * ) -> $ret: ty
311- $body: block
312- ) * ) => ( $(
313- #[ inline]
314- $( #[ $attr] ) *
315- pub $( $constness) * unsafe extern "C" fn $i( $( $arg: $argty) , * ) -> $ret
316- $body
317- ) * )
318- }
319-
320- /// Define a safe function that is const as long as `libc_const_extern_fn` is enabled.
321- macro_rules! safe_f {
322- ( $(
323- $( #[ $attr: meta] ) *
324- pub $( { $constness: ident} ) * fn $i: ident( $( $arg: ident: $argty: ty) , * $( , ) * ) -> $ret: ty
325- $body: block
326- ) * ) => ( $(
327- #[ inline]
328- $( #[ $attr] ) *
329- pub $( $constness) * extern "C" fn $i( $( $arg: $argty) , * ) -> $ret
330- $body
331- ) * )
332- }
333-
334- /// A nonpublic function that is const as long as `libc_const_extern_fn` is enabled.
335- macro_rules! const_fn {
336- ( $(
337- $( #[ $attr: meta] ) *
338- $( { $constness: ident} ) * fn $i: ident( $( $arg: ident: $argty: ty) , * $( , ) * ) -> $ret: ty
339- $body: block
340- ) * ) => ( $(
341- #[ inline]
342- $( #[ $attr] ) *
343- $( $constness) * fn $i( $( $arg: $argty) , * ) -> $ret
344- $body
345- ) * )
346- }
347- } else {
348- /// Define an `unsafe` function that is const as long as `libc_const_extern_fn` is enabled.
349- macro_rules! f {
350- ( $(
351- $( #[ $attr: meta] ) *
352- pub $( { $constness: ident} ) * fn $i: ident( $( $arg: ident: $argty: ty) , * $( , ) * ) -> $ret: ty
353- $body: block
354- ) * ) => ( $(
355- #[ inline]
356- $( #[ $attr] ) *
357- pub unsafe extern "C" fn $i( $( $arg: $argty) , * ) -> $ret
358- $body
359- ) * )
360- }
282+ /// Define a `unsafe` function.
283+ macro_rules! f {
284+ ( $(
285+ $( #[ $attr: meta] ) *
286+ pub $( { $constness: ident} ) ? fn $i: ident( $( $arg: ident: $argty: ty) ,* $( , ) * ) -> $ret: ty
287+ $body: block
288+ ) * ) => ( $(
289+ #[ inline]
290+ $( #[ $attr] ) *
291+ pub $( $constness) ? unsafe extern "C" fn $i( $( $arg: $argty) ,* ) -> $ret
292+ $body
293+ ) * )
294+ }
361295
362- /// Define a safe function that is const as long as `libc_const_extern_fn` is enabled .
363- macro_rules! safe_f {
364- ( $(
365- $( #[ $attr: meta] ) *
366- pub $( { $constness: ident} ) * fn $i: ident( $( $arg: ident: $argty: ty) , * $( , ) * ) -> $ret: ty
367- $body: block
368- ) * ) => ( $(
369- #[ inline]
370- $( #[ $attr] ) *
371- pub extern "C" fn $i( $( $arg: $argty) , * ) -> $ret
372- $body
373- ) * )
374- }
296+ /// Define a safe function.
297+ macro_rules! safe_f {
298+ ( $(
299+ $( #[ $attr: meta] ) *
300+ pub $( { $constness: ident} ) ? fn $i: ident( $( $arg: ident: $argty: ty) ,* $( , ) * ) -> $ret: ty
301+ $body: block
302+ ) * ) => ( $(
303+ #[ inline]
304+ $( #[ $attr] ) *
305+ pub $ ( $constness ) ? extern "C" fn $i( $( $arg: $argty) ,* ) -> $ret
306+ $body
307+ ) * )
308+ }
375309
376- /// A nonpublic function that is const as long as `libc_const_extern_fn` is enabled.
377- macro_rules! const_fn {
378- ( $(
379- $( #[ $attr: meta] ) *
380- $( { $constness: ident} ) * fn $i: ident( $( $arg: ident: $argty: ty) , * $( , ) * ) -> $ret: ty
381- $body: block
382- ) * ) => ( $(
383- #[ inline]
384- $( #[ $attr] ) *
385- fn $i( $( $arg: $argty) , * ) -> $ret
386- $body
387- ) * )
388- }
389- }
310+ /// Define a nonpublic function.
311+ macro_rules! const_fn {
312+ ( $(
313+ $( #[ $attr: meta] ) *
314+ $( { $constness: ident} ) ? fn $i: ident( $( $arg: ident: $argty: ty) ,* $( , ) * ) -> $ret: ty
315+ $body: block
316+ ) * ) => ( $(
317+ #[ inline]
318+ $( #[ $attr] ) *
319+ $( $constness) ? fn $i( $( $arg: $argty) ,* ) -> $ret
320+ $body
321+ ) * )
390322}
391323
392324macro_rules! __item {
0 commit comments