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