2 unstable releases

0.2.0 Jun 16, 2024
0.1.0 Dec 30, 2023

#773 in Rust patterns

Download history 66/week @ 2024-08-15 34/week @ 2024-08-22 57/week @ 2024-08-29 52/week @ 2024-09-05 64/week @ 2024-09-12 46/week @ 2024-09-19 135/week @ 2024-09-26 153/week @ 2024-10-03 296/week @ 2024-10-10 47/week @ 2024-10-17 88/week @ 2024-10-24 41/week @ 2024-10-31 144/week @ 2024-11-07 170/week @ 2024-11-14 49/week @ 2024-11-21 81/week @ 2024-11-28

448 downloads per month
Used in 4 crates (via vyder_core)

MIT/Apache

8KB
52 lines

enum_downcast

GitHub MIT/Apache 2.0 Crates.io docs.rs

Safe downcasting for enums

Example

#[derive(EnumDowncast)]
enum Enum {
    Player(Player),
    Enemy(Enemy),
    Items { vec: Vec<Item> }, // derived code will be identical to if it were `Items(Vec<Items>)`
    #[enum_downcast(skip)]
    Other,
}

let container = Enum::Player(Player { name: "Ryo".to_string() });
let _player_ref: &Player = container.downcast_ref::<Player>().unwrap();
let _player_mut: &mut Player = container.downcast_mut::<Player>().unwrap();
let player: Player = container.downcast::<Player>().unwrap();

You need derive feature to use the derive macro.

You can see more examples in /examples.

My favorite one is one about custom downcast that allows interesting behavior like: https://github.com/ryo33/enum_downcast/blob/75adcbc8d24adb4e9d7b3c873e92bfff0dde7882/examples/partial_custom_impl.rs#L44-L53

Features

  • completely #![no_std]
  • no unsafe code (like std::mem::transmute)
  • no dynamic dispatch
  • #[enum_downcast(skip)] to skip some variants (especially not downcastable ones)
  • enums with type parameters (see example)
  • custom downcast implementation without derive macro (see example)
  • partial custom downcast implementation for some variants with #[enum_downcast(skip)] (see example)
  • compatible with other macros, such as serde, strum, and enum_dispatch (see example with serde and strum and example with enum_dispatch)

There is one limitation: you cannot compile a code that downcasts an enum to any type not listed in the enum definition, because of the lack of the specialization in stable Rust. You can workaround this by using nightly compiler with min_specialization feature like this example. As of specialization stabilized in the future, this limitation will be cleared, and you don't need any boilerplate code like the example.

Dependencies

~105KB