#toggle #flags #feature #flag

features

A macro to generate runtime feature toggles

3 releases (breaking)

Uses old Rust 2015

0.10.0 May 13, 2019
0.9.0 Dec 20, 2017
0.8.0 Mar 20, 2017

#276 in Configuration

Download history 2931/week @ 2023-11-24 1760/week @ 2023-12-01 1430/week @ 2023-12-08 1758/week @ 2023-12-15 742/week @ 2023-12-22 622/week @ 2023-12-29 1487/week @ 2024-01-05 1946/week @ 2024-01-12 1929/week @ 2024-01-19 1640/week @ 2024-01-26 1842/week @ 2024-02-02 1521/week @ 2024-02-09 1748/week @ 2024-02-16 1850/week @ 2024-02-23 1524/week @ 2024-03-01 791/week @ 2024-03-08

6,145 downloads per month
Used in 3 crates

MIT/Apache

15KB
113 lines

Features

Crate version Crate downloads Crate license Documentation

AppVeyor (Windows) Travis (Linux and OS X)

About

features is a small library that implements runtime feature toggles for your library or program allowing behavior to be changed on boot or dynamically at runtime using the same compiled binary artifact. This is different from cargo's feature support which uses conditional compilation.

At its core, it is a macro (features!) that takes a collection of feature flag names which it uses to generate a module containing a function to enable a feature toggle (::enable()), a function to disable a feature toggle (::disable()) and a function to check if a feature toggle is enabled (::is_enabled()).

Example

Basic example:

#[macro_use]
extern crate bitflags;
#[macro_use]
extern crate features;

features! {
    pub mod feature {
        const Alpha = 0b00000001,
        const Beta = 0b00000010
    }
}

fn main() {
    assert_eq!(false, feature::is_enabled(feature::Alpha));
    assert_eq!(false, feature::is_enabled(feature::Beta));

    feature::enable(feature::Beta);
    assert_eq!(false, feature::is_enabled(feature::Alpha));
    assert_eq!(true, feature::is_enabled(feature::Beta));
}

Multiple feature sets:

#[macro_use]
extern crate bitflags;
#[macro_use]
extern crate features;

features! {
    pub mod ux {
        const JsonOutput = 0b10000000,
        const VerboseOutput = 0b01000000
    }
}

features! {
    pub mod srv {
        const Http2Downloading = 0b10000000,
        const BitTorrentDownloading = 0b01000000
    }
}

fn main() {
    // Parse CLI args, environment, read config file etc...
    srv::enable(srv::BitTorrentDownloading);
    ux::enable(ux::JsonOutput);

    if srv::is_enabled(srv::Http2Downloading) {
        println!("Downloading via http2...");
    } else if srv::is_enabled(srv::BitTorrentDownloading) {
        println!("Downloading via bit torrent...");
    } else {
        println!("Downloading the old fashioned way...");
    }

    if ux::is_enabled(ux::VerboseOutput) {
        println!("COOL");
    }
}

Feature Toggle References

Here are some articles and projects which insipred the implementation of features:

License

Features is licensed under the Apache License, Version 2.0 and the MIT license. Please read the LICENSE-APACHE and LICENSE-MIT for details

Dependencies

~83KB