#rustc-version #version #rustc #check

no-std if_rust_version

Macro to enable or disable code depending on the rust version

2 releases (1 stable)

Uses old Rust 2015

1.0.0 Sep 6, 2019
0.1.0 Sep 1, 2019

#1667 in Development tools

Download history 225/week @ 2024-03-13 258/week @ 2024-03-20 287/week @ 2024-03-27 264/week @ 2024-04-03 263/week @ 2024-04-10 254/week @ 2024-04-17 297/week @ 2024-04-24 271/week @ 2024-05-01 245/week @ 2024-05-08 258/week @ 2024-05-15 247/week @ 2024-05-22 251/week @ 2024-05-29 205/week @ 2024-06-05 171/week @ 2024-06-12 220/week @ 2024-06-19 163/week @ 2024-06-26

795 downloads per month
Used in qmetaobject

MIT/Apache

14KB
188 lines

if_rust_version

Build Status Crates.io docs Minimum Rust version: 1.12

This is a small crate that just export one macro that allow you to have code conditionally of the rust version. The if_rust_version! macro allows you to still support older version of rustc while still using conditionally new features of the compiler

Examples

The release of rust 1.36 stabilized the MaybeUninit union as a replacement for the to be deprecated mem::uninitialized. One might want to use MaybeUninit when the compiler supports it, but not without requiring a recent compiler.

The if_rust_version! macro is exactly what one needs:

if_rust_version! { >= 1.36 {
    let mut x = std::mem::MaybeUninit::<u32>::uninit();
    unsafe { x.as_mut_ptr().write(32); }
    let xx = unsafe { x.assume_init() };
} else {
    let mut xx : u32 = unsafe { mem::uninitialized() };
    unsafe { ptr::write(&mut xx as *mut u32, 32); }
}}

The macro can be used to declare items or expression.

It can also be usefull to declare macro for pattern you often use. For example, if we want to declare functions const from rust 1.31 which introduced the concept:

if_rust_version! { >= 1.31 {
    // just a identity macro that forward the item
    macro_rules! const_fn { ($f:item) => { $f } }
} else {
    // remove the 'const'
    macro_rules! const_fn {
        ($(#[$m:meta])* const fn $($rest:tt)*) => { $(#[$m])* fn $($rest)* };
        ($(#[$m:meta])* pub const fn $($rest:tt)*) => {
            $(#[$m])*
            /// This function is a const fn from rust 1.31
            pub fn $($rest)*
        };
    }
}}

const_fn!{
    /// This function is const chen the compiler supports it
    pub const fn hello(x : u32) -> u32 { x + 2 }
}

Minimum Rust version

The minimum rust version is rust 1.12, because previous versions were not expanding tt correcrtly in macro_rules!

This crate has no dependencies, and is #![no-std].

Comparison with other crates

There are other crates that check the rust version number:

The main difference with version_check and rustc_version is that these crates are meant to to be used by first writing a build.rs script to then add some feature flag.

rustversion is a attribute macro, this has the disadvantage not to be able to be used for feature which add new grammar to the language (example: ? or 10u128 or impl Trait). Also attribute macros were only stabilized recently, and do not support expanding to expressions yet. Not to mention the need to use heavy dependencies such as syn to develop procedural macro.

There is also a proposed RFC 2523 which suggest allowing to query the rust version via the #[cfg(...)] attributes.

Licenses:

MIT/Apache-2.0

No runtime deps