34 个版本 (14 个稳定版)
使用旧版 Rust 2015
| 1.5.3 | 2022 年 9 月 2 日 |
|---|---|
| 1.5.2 | 2021 年 10 月 2 日 |
| 1.4.2 | 2021 年 8 月 15 日 |
| 1.4.1 | 2021 年 5 月 22 日 |
| 0.1.4 | 2018 年 10 月 6 日 |
#218 in Rust 模式
27,451 次月下载
在 47 个crate中使用 (13 个直接使用)
455KB
6K SLoC
许多标准/核心库类型/特质的扩展特质,以及其他杂项类型/特质/函数/宏。
添加依赖项
此 crate 需要使用 cargo 特性来启用项,要获取所有项,可以使用
[dependencies.core_extensions]
version = "1.5"
features = [
# enables items that use anything from the standard `std` or `alloc` crates.
"std",
# Requires the latest stable release, enables all the rust-version-dependent features
"rust_latest_stable",
# enables all the item features
"all_items",
]
需要 "std" 特性来启用使用 std 类型的 impl 和项,否则只支持 core 库。
"rust_latest_stable" 启用所有 "rust_1_*" crate 特性以使用最新的稳定语言特性,这里列出了所有 "rust_1_*" 特性,
"all_items" 启用此 crate 中所有项的特性和功能 (在此处记录)
以下是上述配置的扩展版本
[dependencies.core_extensions]
version = "1.5"
features = [
"std",
"rust_latest_stable"
# all of the features below are what "all_items" enables
"derive"
"bools",
"callable",
"collections",
"const_default",
"const_val",
"generics_parsing",
"integers",
"item_parsing",
"iterators",
"macro_utils",
"marker_type",
"on_drop",
"option_result",
"phantom",
"self_ops",
"slices",
"strings",
"transparent_newtype",
"type_asserts",
"type_identity",
"type_level_bool",
"void",
]
示例
展示此 crate 的一些功能。
quasiconst,泛型常量。
quasiconst 宏允许通过生成一个零大小的泛型类型并实现 ConstVal 特质来模拟泛型常量,获取其值的首选方法是 getconst 宏。
此示例演示了如何使用它们来声明一个泛型 VTABLE 常量。
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>,
};
}
fn main() {
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)
}
Cargo 特性
项特性
项特性启用此 crate 中的项。
“"all_items"”特性启用了所有这些功能,如果您不介意编译时间更长,可以使用它来替代以下特性。
“"all_items_no_derive"”特性启用了以下所有功能,除了“"derive"”功能,以减少由于启用“syn”间接依赖而导致的编译时间。
-
"derive":启用在core_extensions中声明的特质的derive宏。如果一个特质有derive宏,它将提及并链接到它。 -
"bools":启用了BoolExt特质,这是bool类型的扩展特质。 -
"callable":启用了callable模块,其中包含稳定可实现的Fn*特质的等效实现。 -
"collections":启用了collections模块,其中包含集合类型的特质。 -
"const_default":启用了ConstDefault特质和const_default宏,以提供与Default特质等效的const。 -
"const_val":启用了ConstVal特质(用于表示常数的类型),getconst宏(用于获取ConstVal::VAL关联常量),和quasiconst宏(用于声明模拟泛型常量的类型)。启用“"generics_parsing"特性。 -
"macro_utils:启用rewrap_macro_parameters、count_tts、gen_ident_range、tokens_method、compile_error_stringify和parenthesize_args宏。同时启用macro_attr属性。 -
"generics_parsing":启用parse_generics、parse_generics_and_where、split_generics_and_where、parse_split_generics和parse_split_generics_and_where宏。这些宏允许宏解析具有泛型参数的项目。 -
"item_parsing":启用"macro_utils和"generics_parsing"功能。启用impl_parse_generics和impl_split宏。 -
"integers":启用integers模块,包含整数类型的扩展特质。 -
"iterators":启用iterators模块,包含迭代器的IteratorExt扩展特质和少数迭代器类型。 -
"marker_type":启用MarkerType特质,用于可以简单构造、零大小和对齐到1的类型。 -
"on_drop":启用RunOnDrop类型,这是一个包装类型,它在作用域结束时运行闭包。 -
"option_result":启用option_result_ext模块,包含针对Option和Result类型的特质。 -
"phantom":启用phantom模块(与PhantomData相关的项目),expr_as_phantom宏,map_phantomdata宏,以及return_type_phantom宏。 -
"self_ops":启用SelfOps特性,这是一个适用于所有类型的扩展特性。它主要用于调用自由函数作为方法。 -
"slices":启用slices模块,为[T]和str切片提供扩展特性。 -
"transparent_newtype":启用transparent_newtype模块,为具有公共字段的#[repr(transparent)]新类型提供扩展特性和函数。
启用 `"marker_type"` 功能。
-
"type_asserts":启用type_asserts模块,包含类型级断言,在测试中最有用。 -
"type_identity":启用TypeIdentity特性,用于证明两个类型相等,并在泛型上下文中进行类型之间的转换。 -
"type_level_bool":启用type_level_bool模块,它将bool编码在类型级别上。 -
"void":启用Void类型,一个无法构造的类型,用于编码不可能的情况。
Rust 版本号
这些特性使代码能够使用高于最低支持版本的 Rust 版本的代码。
-
"rust_1_46": 实现了关联函数
TransparentNewtype和TypeIdentity,这些函数接受Rc<Self>或Arc<Self>可调用作为方法。 -
"rust_1_51": 启用 "rust_1_46" 功能,并为所有数组长度实现特质。
-
"rust_latest_stable": 启用所有 "rust_1_*" 功能。这需要 Rust 的最新稳定版,因为随时可以添加更多的
"rust_1_*"功能。
对其他包的支持
默认情况下,所有这些功能都是禁用的
-
"std": 启用std库支持。隐含"alloc"功能。 -
"alloc": 启用alloc库支持。 -
"serde_": 启用 serde 支持。
其他特性
"track_caller": 启用 "rust_1_46" 功能。将 ResultLike 更改为允许在 ResultLike::into_result_ 中获取调用者位置,并使 IsNoneError 存储其构造位置。
"docsrs": 用于在 docs.rs 中记录所需特性,需要 Rust 夜间版本。本身不启用任何项目。
无标准支持
此包默认在 #![no_std] 环境中工作。
支持的 Rust 版本
此包支持 Rust 返回到 1.41.0,需要 cargo 功能来使用较新版本的编程语言功能。
许可
core_extensions 许可证受以下其中之一
Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
您自行选择。
贡献
除非您明确说明,否则您提交给 core_extensions 的任何有意贡献,根据 Apache-2.0 许可证定义,应按上述方式双重许可,不附加任何额外条款或条件。
依赖项
~0–280KB