prefixes
Important note
This project is currently only an experiment. At this point, it is highly uncertain if the project will be continued or dropped soon.See the on-going discussion on literal prefixes here
Description
Prefixes delivers various prefix-like proc macro attributes for literals to easily create common types.
Quick example:
use ;
Usage
To use prefixes, first add it to your project in Cargo.toml:
[]
= "0.1.0"
Then enable stmt_expr_attributes and proc_macro_hygiene features (this is possible only on nightly Rust):
Finally, use prefixes that you need, e.g.
use f;
Prefixes
Prefix #[f]
Build formatted string from a string literal using the format! macro.
use f;
let n = 2137;
let s1 = format!;
let s2 = "n = {n}";
assert_eq!;
Might be also useful for creating owned strings, e.g.
use f;
let s1 = "2137";
let s2 = "2137".to_string;
assert_eq!;
Prefixes #[ms], #[s]
Build builds std::time::Duration from an integer literal using from_millis (#[ms]) or from_secs[_f32|_f64] methods (#[s]).
use Duration;
use ;
let d1 = from_millis;
let d2 = 1000;
assert_eq!;
let d3 = from_secs;
let d4 = 2;
assert_eq!;
let d5 = from_secs_f32;
let d6 = 3.0f32;
assert_eq!;
let d7 = from_secs_f64;
let d8 = 4.0f64;
assert_eq!;
Prefixes #[os], #[OS]
Build OsStr (#[os]) or OsString (#[OS]) from a string literal. Additionally, #[OS] supports string interpolation like #[f].
use ;
use ;
let os1 = new;
let os2 = "foo";
assert_eq!;
let n = 42;
let os3 = from;
let os4 = "n = 42";
assert_eq!;
Prefixes #[p], #[P]
Build Path (#[p]) or PathBuf (#[P]) from a string literal. Additionally, #[P] supports string interpolation.
use ;
use ;
let p1 = new;
let p2 = "/foo";
assert_eq!;
let ext = "txt";
let p3 = from;
let p4 = "/foo.{ext}";
assert_eq!;
Prefixes #[re], #[RE]
Build Regex from a string literal. Additionally, #[RE] calls .unwrap() on the result. Works only if regex crate is included in the dependencies. Doesn't require explicit use regex::Regex.
use Regex;
use ;
let re1 = new;
let re2 = "1|2";
assert_eq!;
let re3 = new.unwrap;
let re4 = "[A-Z]";
assert_eq!;
License
This project is licensed under the MIT License - see the LICENSE file for details.