#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

#1978 in Rust patterns

Download history 29849/week @ 2023-12-11 23241/week @ 2023-12-18 13564/week @ 2023-12-25 19058/week @ 2024-01-01 23608/week @ 2024-01-08 24711/week @ 2024-01-15 25526/week @ 2024-01-22 29748/week @ 2024-01-29 30514/week @ 2024-02-05 30290/week @ 2024-02-12 32088/week @ 2024-02-19 32544/week @ 2024-02-26 35069/week @ 2024-03-04 38156/week @ 2024-03-11 36566/week @ 2024-03-18 32335/week @ 2024-03-25

144,366 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