#traits #downcast #object #unsafe #unchecked #type #reference

unsafe-any-ors

Traits and implementations for unchecked downcasting

1 stable release

Uses old Rust 2015

1.0.0 Sep 3, 2022

#2005 in Rust patterns

Download history 36500/week @ 2024-03-15 34745/week @ 2024-03-22 33181/week @ 2024-03-29 34812/week @ 2024-04-05 36847/week @ 2024-04-12 40060/week @ 2024-04-19 40676/week @ 2024-04-26 39133/week @ 2024-05-03 42643/week @ 2024-05-10 39424/week @ 2024-05-17 39835/week @ 2024-05-24 44318/week @ 2024-05-31 47953/week @ 2024-06-07 41349/week @ 2024-06-14 36860/week @ 2024-06-21 28541/week @ 2024-06-28

163,367 downloads per month
Used in 7 crates (via typemap-ors)

MIT license

8KB
101 lines

unsafe-any-ors CI

Convenience traits for unsafe downcasting from trait objects to concrete types.

Overview

This crate defines two new traits UncheckedAnyDowncast and UncheckedAnyMutDowncast, which define methods for downcasting to any type that implements Any from implemented trait objects.

It also defines two convenience implementations of these traits for &'a Any and &'a mut Any, which are the most common trait objects that you might downcast from.

Example:

let a = box 7u as Box<Any>;
unsafe { assert_eq!(*a.downcast_ref_unchecked::<uint>(), 7u); }

License

MIT


lib.rs:

Traits for unsafe downcasting from trait objects to & or &mut references of concrete types. These should only be used if you are absolutely certain of the type of the data in said trait object - there be dragons etc.

Originally inspired by https://github.com/chris-morgan/anymap and the implementation of std::any::Any.

Dependencies

~8KB