Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::any::Any;
- use std::sync::Arc;
- trait FooTrait: Any {
- fn method(&self) -> u32;
- }
- struct Foo {
- value: u32,
- }
- impl FooTrait for Foo {
- fn method(&self) -> u32 {
- self.value
- }
- }
- fn new_foo() -> Option<Arc<FooTrait + Send + Sync + 'static>> {
- Some(Arc::new(Foo { value: 99 }))
- }
- fn test(value: Arc<dyn Any + Send + Sync + 'static>) {
- if let Ok(s) = value.downcast::<Foo>() {
- println!("{}", s.method());
- }
- }
- type Object = Arc<dyn Any + Send + Sync + 'static>;
- fn main() {
- let cast: Option<Object> = new_foo().map(|c| {
- c.downcast::<>()
- });
- test(cast);
- test(Arc::new(1));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement