2 releases
0.1.1 | Mar 1, 2022 |
---|---|
0.1.0 | Feb 28, 2022 |
#20 in #downcast
Used in 5 crates
(4 directly)
30KB
511 lines
用于将trait object向下造型为具体类型
lib.rs
:
https://github.com/fkoep/downcast-rs 该库参考了downcast-rs, 为Box、Rc、Arc实现了downcast接口(向下造型)
为什么不直接使用downcast-rs?
downcast-rs仅为Box、Rc提供了downcast
,未提供Arc的downcast
方法
补充:你不应该再使用本库,后续可能删除本库,因为最新的downcast-rs已经提供了Arc的downcast接口(2021.5.21)
下面的注释,拷贝了downcast-rs的README.md, 并不说明本库的用法。 Rust enums are great for types where all variations are known beforehand. But in the case where you want to implement a container of user-defined types, an open-ended type like a trait object is needed. In some cases, it is useful to cast the trait object back into its original concrete type to access additional functionality and performant inlined implementations.
downcast-rs
adds downcasting support to trait objects using only safe Rust. It
supports type parameters, associated types, and constraints.
To make a trait downcastable, make it extend the any::BoxAny
trait and
invoke impl_downcast!
on it as follows:
trait Trait: BoxAny {}
impl_downcast!(Trait);
// With type parameters.
trait TraitGeneric1<T>: BoxAny {}
impl_downcast!(TraitGeneric1<T>);
// With associated types.
trait TraitGeneric2: BoxAny { type G; type H; }
impl_downcast!(TraitGeneric2 assoc G, H);
// With constraints on types.
trait TraitGeneric3<T: Copy>: BoxAny {
type H: Clone;
}
impl_downcast!(TraitGeneric3<T> assoc H where T: Copy, H: Clone);
// With concrete types.
trait TraitConcrete1<T: Copy>: BoxAny {}
impl_downcast!(concrete TraitConcrete1<u32>);
trait TraitConcrete2<T: Copy>: BoxAny { type H; }
impl_downcast!(concrete TraitConcrete2<u32> assoc H=f64);
Example without generics
// Import macro via `macro_use` pre-1.30.
#[macro_use]
extern crate any;
use any::BoxAny;
// To create a trait with downcasting methods, extend `BoxAny` and run
// `impl_downcast!()` on the trait.
trait Base: BoxAny {}
impl_downcast!(Base);
// Concrete types implementing Base.
#[derive(Debug)]
struct Foo(u32);
impl Base for Foo {}
#[derive(Debug)]
struct Bar(f64);
impl Base for Bar {}
fn main() {
// Create a trait object.
let mut base: Box<Base> = Box::new(Foo(42));
// Try sequential downcasts.
if let Some(foo) = base.downcast_ref::<Foo>() {
assert_eq!(foo.0, 42);
} else if let Some(bar) = base.downcast_ref::<Bar>() {
assert_eq!(bar.0, 42.0);
}
assert!(base.is::<Foo>());
// Fail to convert `Box<Base>` into `Box<Bar>`.
let res = base.downcast::<Bar>();
assert!(res.is_err());
let base = res.unwrap_err();
// Convert `Box<Base>` into `Box<Foo>`.
assert_eq!(42, base.downcast::<Foo>().map_err(|_| "Shouldn't happen.").unwrap().0);
}
Example with a generic trait with associated types and constraints
// Can call macro via namespace since rust 1.30.
extern crate any;
use any::BoxAny;
// To create a trait with downcasting methods, extend `BoxAny` and run
// `impl_downcast!()` on the trait.
trait Base<T: Clone>: BoxAny { type H: Copy; }
downcast_rs::impl_downcast!(Base<T> assoc H where T: Clone, H: Copy);
// or: impl_downcast!(concrete Base<u32> assoc H=f32)
// Concrete types implementing Base.
struct Foo(u32);
impl Base<u32> for Foo { type H = f32; }
struct Bar(f64);
impl Base<u32> for Bar { type H = f32; }
fn main() {
// Create a trait object.
let mut base: Box<Base<u32, H=f32>> = Box::new(Bar(42.0));
// Try sequential downcasts.
if let Some(foo) = base.downcast_ref::<Foo>() {
assert_eq!(foo.0, 42);
} else if let Some(bar) = base.downcast_ref::<Bar>() {
assert_eq!(bar.0, 42.0);
}
assert!(base.is::<Bar>());
}