Crate powerletters
Power Letters for Rust
Concise spellings of common Rust operations:
C-CloneO-ToOwnedS-ToStringI- IgnoreResultX-expectforResultandOption
All operations are provided as both functions and methods. Sometimes one reads better than the other.
In action:
# use powerletters::*;
# use std::path::Path;
# struct PathGen;
# impl PathGen {
# fn from_path(&self, _: &Path) -> Option<String> { Some("path".to_string()) }
# }
# let path = Path::new("a/b/c");
# let pathgen = PathGen;
let mut parents: Vec<_> = path.ancestors().skip(1).map(|path| {
if path == Path::new("") {
let parent_path = pathgen.from_path(path).X();
let parent_label = S("<root>");
(parent_path, parent_label)
} else {
let parent_path = pathgen.from_path(path).X();
let parent_label = path.iter().last().X().to_string_lossy().S();
(parent_path, parent_label)
}
}).collect();
Power Clone
use powerletters::*;
let bagostuff = vec!["a", "b", "c"];
let newbag = bagostuff.C();
// or
let newbag = C(&bagostuff);
Power ToOwned
use powerletters::*;
use std::path::Path;
let yourpath = Path::new("chill");
let mypath = yourpath.O();
// or
let mypath = O(yourpath);
Power ToString
use powerletters::*;
let s: String = S("foo");
// or
let s: String = "foo".S();
Power ignore Result - kick that Result to the curb!
use powerletters::*;
use std::io::Write;
let mut buf = Vec::new();
write!(&mut buf, "hello").I();
// or
I(write!(&mut buf, "world"));
Note this is superior to let _ = ...
because the let version is untyped and can
introduce unintended bugs like ignoring futures.
Power expect for Result and Option.
use powerletters::*;
# use std::io;
let maybe_thing = Some("thing");
let thing = maybe_thing.X(); // like `.expect("some baloney")`
let good_thing: Result<_, io::Error> = Ok("thing");
let thing = good_thing.X();
// or
let maybe_thing = Some("thing");
let thing = X(maybe_thing);
let good_thing: Result<_, io::Error> = Ok("thing");
let thing = X(good_thing);
Panics with helpful messages:
# use powerletters::*;
let none: Option<i32> = None;
let value = none.X();
// thread 'main' panicked at src/main.rs:3:19:
// impossible `None` option
# use powerletters::*;
let err: Result<i32, _> = Err("something went wrong");
let value = err.X();
// thread 'main' panicked at src/main.rs:3:18:
// impossible `Err` result: something went wrong
Traits
-
PowerClone
Power
Clone. -
PowerExpect
Power
expectforResultandOption. -
PowerToOwned
Power
ToOwned. -
PowerToString
Power
ToString. -
ResultIgnore
Power ignore
Result- kick thatResultto the curb!