Crate core_extensions[−][src]
Extension traits for many standard/core library types/traits. and other miscelaneuous types / traits / functions / macros.
Adding as dependency
This crate requires cargo features for enabling items, to get all of them you can use:
[dependencies.core_extensions]
version = "1.0"
features = ["std", "all_items"]
The “std” feature is required to enable impls and items that use std types,
otherwise only the core library is supported.
For enabling features individually, look here.
This crate currently requires cargo features to use newer language features,
Examples
Showcasing some features from this crate.
quasiconst, generic constants.
The quasiconst macro allows emulating generic constants by generating a 
zero-sized generic type that implements the ConstVal trait,
the preferred way to get its value is the getconst macro.
This example demonstrates how you can use them to declare a generic VTABLE constant.
use core_extensions::{getconst, quasiconst}; use std::fmt::{self, Debug}; quasiconst!{ pub const VTABLE<T: Debug>: &'static Vtable = &Vtable { size: std::mem::size_of::<T>(), align: std::mem::align_of::<T>(), drop: drop_erased::<T>, fmt: debug_fmt_erased::<T>, }; } const VTABLE_U8: &'static Vtable = getconst!(VTABLE<u8>); assert_eq!(VTABLE_U8.size, 1); assert_eq!(VTABLE_U8.align, 1); const VTABLE_USIZE: &'static Vtable = getconst!(VTABLE<usize>); assert_eq!(VTABLE_USIZE.size, std::mem::size_of::<usize>()); assert_eq!(VTABLE_USIZE.align, std::mem::align_of::<usize>()); const VTABLE_STRING: &'static Vtable = getconst!(VTABLE<&str>); assert_eq!(VTABLE_STRING.size, std::mem::size_of::<usize>() * 2); assert_eq!(VTABLE_STRING.align, std::mem::align_of::<usize>()); pub struct Vtable { pub size: usize, pub align: usize, pub drop: unsafe fn(*mut ()), pub fmt: unsafe fn(*const (), &mut fmt::Formatter<'_>) -> fmt::Result, } unsafe fn drop_erased<T>(ptr: *mut ()) { std::ptr::drop_in_place(ptr as *mut T) } unsafe fn debug_fmt_erased<T>(ptr: *const (), f: &mut fmt::Formatter<'_>) -> fmt::Result where T: Debug, { let this = unsafe{ &*(ptr as *const T) }; Debug::fmt(this, f) }
no-std support
This crate works in #![no_std] contexts by default.
Supported Rust versions
This crate support Rust back to 1.41.0, requiring cargo features to use language features from newer versions.
Cargo Features
crate features
The "all_items" feature enables all of these features,
you can use it instead of the ones below if you don’t mind longer compile-times:
- 
"bools": Enables theBoolExttrait, extension trait forbool. - 
"callable": Enables thecallablemodule, with stably implementable equivalents of theFn*traits. - 
"collections": Enables thecollectionsmodule, with traits for collection types. - 
"const_default": Enables theConstDefaulttrait, andconst_defaultmacro, for aconstequivalent of theDefaulttrait. - 
"const_val": Enables theConstValtrait (for types that represent constants),getconstmacro (for getting theConstVal::VALassociated constant), andquasiconstmacro (for declaring types that emulate generic constants). Enables the"generics_parsing"feature. - 
"generics_parsing": Enables theparse_generics,parse_generics_and_where, andsplit_generics_and_wheremacros. These allow macros to parse items with generic parameters. - 
"integers": Enables theintegersmodule, with extension traits for integer types. - 
"iterators": Enables theiteratorsmodule, with theIteratorExtextension trait for iterators, and a few iterator types. - 
"marker_type": Enables theMarkerTypetrait, for trivially constructible, zero-sized, and aligned-to-1 types. - 
"on_drop": Enables theRunOnDroptype, a wrapper type that runs a closure at the end of the scope. - 
"option_result": Enables theoption_result_extmodule, with traits forOptionandResult-like types. - 
"phantom": Enables thephantommodule(withPhantomData-related items),expr_as_phantommacro,map_phantomdatamacro, andreturn_type_phantommacro. - 
"self_ops": Enables theSelfOpstrait, an extension trait for all types. It primarily has methods for calling free functions as methods. - 
"slices": Enables theslicesmodule, with extension traits for[T]andstrslices. - 
"strings": Enables thestringsmodule, with theStringExtextension trait for strings. - 
"transparent_newtype": Enables thetransparent_newtypemodule, with extension traits and functions for#[repr(transparent)]newtypes with public fields. - 
"type_asserts": Enables thetype_assertsmodule, with type-level assertiosn, most useful in tests. - 
"type_identity": Enables theTypeIdentitytrait, for proving that two types are equal, and converting between them in a generic context. - 
"type_level_bool": Enables thetype_level_boolmodule, which encodesbools on the type-level. - 
"void": Enables theVoidtype, a type that can’t be constructed, for encodign impossible situations. 
Rust Version numbers
These features enable code that require some Rust version past the minimum supported one:
- 
“rust_1_46”: Makes
TransparentNewtypeandTypeIdentityassociated functions that takeRc<Self>orArc<Self>callable as methods. - 
“rust_1_51”: Enables the “rust_1_46” feature, and impls of traits for all array lengths.
 
Support for other crates
All of these are disabled by default:
- 
"std": Enablesstdlibrary support. Implies the"alloc"feature. - 
"alloc": Enablesalloclibrary support. - 
"serde_": Enables serde support. 
Miscelaneous features
"track_caller":
Enables the “rust_1_46” feature.
Changes ResultLike to allow getting the caller location in ResultLike::into_result_,
and makes IsNoneError store where it was constructed.
"docsrs": Used to document the required features in docs.rs, requires Rust nightly.
Doesn’t enable any items itself.
Re-exports
pub use self::callable::CallExt; | 
pub use self::callable::CallInto; | 
pub use self::callable::CallMut; | 
pub use self::callable::CallRef; | 
pub use self::integers::IntegerExt; | 
pub use self::integers::ToTime; | 
pub use self::iterators::IterCloner; | 
pub use self::iterators::IterConstructor; | 
pub use self::iterators::IteratorExt; | 
pub use self::iterators::LazyOnce; | 
pub use self::option_result_ext::OptionExt; | 
pub use self::option_result_ext::ResultExt; | 
pub use self::option_result_ext::ResultLike; | 
pub use self::option_result_ext::ResultLikeExt; | 
pub use self::option_result_ext::TransposeOption; | 
pub use self::phantom::AsPhantomData; | 
pub use self::phantom::AndPhantom; | 
pub use self::phantom::AndPhantomCov; | 
pub use self::phantom::as_phantom; | 
pub use self::phantom::as_covariant_phantom; | 
pub use self::phantom::ContraVariantPhantom; | 
pub use self::phantom::InvariantPhantom; | 
pub use self::phantom::InvariantRefPhantom; | 
pub use self::phantom::VariantDropPhantom; | 
pub use self::phantom::CovariantPhantom; | 
pub use self::strings::StringExt; | 
pub use self::slices::ValSliceExt; | 
pub use self::slices::SliceExt; | 
pub use self::transparent_newtype::TransparentNewtype; | 
pub use self::transparent_newtype::TransparentNewtypeExt; | 
Modules
| callable | callableAlternatives of the standard   | 
| collections | collectionsExtension traits for collection types.  | 
| integers | integersExtension traits for integers and types used in the traits.  | 
| iterators | iteratorsIterator adaptors and constructors.  | 
| measure_time | Time measurement.  | 
| option_result_ext | option_resultContains extension traits for Option and Result  | 
| phantom | phantom
  | 
| slices | slicesSlice extension traits, and related items.  | 
| strings | slicesExtension trait for string types.  | 
| transparent_newtype | transparent_newtypeTraits for newtype wrappers.  | 
| type_asserts | type_assertsType-level assertions, most useful for tests.  | 
| type_level_bool | type_level_boolType level booleans  | 
| utils | Miscelaneous utility functions  | 
Macros
| const_default | const_defaultGets the   | 
| expr_as_phantom | phantomGets the type of an expression as a   | 
| getconst | const_valGets the   | 
| impl_call | callableThis macro allows more ergonomically implementing the 
  | 
| impl_transparent_newtype | transparent_newtypeFor implementing the   | 
| iter_cloner | iteratorsUse this macro to create an
  | 
| map_phantomdata | phantomMaps a   | 
| matches | Evaluates to true if the expression matches any of the patterns (this macro can have multiple patterns).  | 
| parse_generics | generics_parsingParses a list of generic parameters, and passes them to a macro.  | 
| parse_generics_and_where | generics_parsingFor writing macros that parse item definitions. This also parses generics for using them in all syntactic locations they’re usable in.  | 
| quasiconst | const_valDeclare types that emulate generic constants.  | 
| return_type_phantom | phantomGets the return type of a parameterless closure as a   | 
| split_generics_and_where | generics_parsingFor writing macros that parse item definitions, while treating generics opaquely.  | 
Structs
| RunOnDrop | on_dropA wrapper type that runs a closure at the end of the scope.  | 
Enums
| Void | voidType for impossible situations.  | 
Traits
| BoolExt | boolsExtension trait for   | 
| ConstDefault | const_defaultA const equivalent of the   | 
| ConstVal | const_valFor types that represent constants.  | 
| MarkerType | marker_typeRepresents a zero-sized marker type .  | 
| SelfOps | self_opsExtension trait for every type.  | 
| TypeIdentity | type_identityAllows converting   | 
Type Definitions
| TIdentity | type_identityA type-level identity function  |