Expand description
Additional features for the Rust standard library.
§Description
This crate contains enhancements to the Rust standard library types, useful for
broad audience, but not yet implemented (or stabilized) in std.
Crate is designed to be lightweight (no external dependencies!) and provide essential
functionality which possible can get to the std some day.
§Extension traits
All the new functionality the stanard library is added using extension traits.
Below you can find the table of all the extension traits introduced by this crate:
std structure | extension traits |
|---|---|
Vec | VecExt and VecExtClone |
&str | StrExt |
Option | OptionExt |
Result | ResultExt |
Duration | DurationExt |
RwLock | RwLockExt |
Mutex | MutexExt |
f32 and f64 | FloatConvert |
§Integer super-trait
While all built-in integer types have mostly the same interface, it’s not backed by any trait, which makes it impossible to write a function that will accept any built-in integer.
Integer trait solves that problem by reflecting the common interface of all the built-in integers.
§Macros
Another group of extensions in stdext is new macros:
compile_warningfor spawning a user-defined compilation warnings.function_namefor getting an enclosing function name.debug_namefor getting a helpful string for debug.
§Highlights
-
Convenient builder methods for
Duration:use std::time::Duration; use stdext::prelude::*; let duration = Duration::from_minutes(1).add_secs(5).add_micros(100); assert_eq!(duration, Duration::new(65, 100_000)); -
Panicking version for
RwLock::read,RwLock::writeandMutex::lock(for fellows who don’t really handle the lock poisoning):use std::sync::{Arc, RwLock}; use stdext::prelude::*; let lock = Arc::new(RwLock::new(1)); { let mut n = lock.force_write(); // Instead of `.write().unwrap()`. *n = 2; } let n = lock.force_read(); assert_eq!(*n, 2); -
Result::combineandOption::combineto zip pairs of objects:use stdext::prelude::*; let x = Some(1); let y = Some("hi"); let z = None::<u8>; assert_eq!(x.combine(y), Some((1, "hi"))); assert_eq!(x.combine(z), None); let x = Ok(1); let y = Ok("hi"); let z: Result<i32, &str> = Err("error"); let z2: Result<i32, &str> = Err("other_error"); assert_eq!(x.combine(y), Ok((1, "hi"))); assert_eq!(x.combine(z), Err("error")); assert_eq!(z.combine(z2), Err("error")); -
New handy macros (mostly for development purposes):
use stdext::{compile_warning, debug_name, function_name}; fn sample_function() { println!("This function is called {}", function_name!()); println!("You can also found it here: {}", debug_name!()); compile_warning!("This function must do something else..."); }
Modules§
- default
- Freestanding version of
std::default::Default::default(). - duration
- Extension traits for
std::time::Duration. - macros
- Various helper macros.
- num
- Extensions for the built-in numeric types.
- option
- Extension traits for
std::Option. - prelude
- A “prelude” module which re-exports all the extension traits for the simple library usage.
- result
- Extension traits for
std::Result. - str
- Extension traits for
strprimitive type. - sync
- Extension traits for the standard synchronization primitives.
- vec
- Extension traits for
std::Vec.
Macros§
- compile_
warning compile_warningmacro is a brother ofstd::compile_error, which emits a compile-time warning with a provided message.- debug_
name - This macro returns the name of the enclosing function, line number and also filename.
As the internal implementation is based on the
std::any::type_name, this macro derives all the limitations of this function. - function_
name - This macro returns the name of the enclosing function.
As the internal implementation is based on the
std::any::type_name, this macro derives all the limitations of this function. - return_
ok - Checks whether supplied
Resultvariable isOkand if so, returns it. - return_
some - Checks whether supplied
Optionvariable isSomeand if so, returns it. - try_
match - Attempts to get variant from the enum variable.
- unwrap_
match - Similar to
try_matchbut additionally unwraps the result.