#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

#1828 in Rust patterns

Download history 49425/week @ 2024-07-20 51316/week @ 2024-07-27 49166/week @ 2024-08-03 54454/week @ 2024-08-10 52369/week @ 2024-08-17 47496/week @ 2024-08-24 45637/week @ 2024-08-31 49842/week @ 2024-09-07 42272/week @ 2024-09-14 45507/week @ 2024-09-21 42267/week @ 2024-09-28 44368/week @ 2024-10-05 40724/week @ 2024-10-12 47879/week @ 2024-10-19 38734/week @ 2024-10-26 47868/week @ 2024-11-02

181,961 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