Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::collections::HashMap;
- use std::rc::Rc;
- use std::any::Any;
- use std::marker::PhantomData;
- // #[derive(From)] with derive_more
- enum AttributeData {
- FloatPair((f32, f32)),
- Int(u32),
- }
- struct Attribute<T> {
- name: String,
- data: AttributeData,
- _phantom: PhantomData<T>,
- }
- impl<T: Into<AttributeData>> Attribute<T> {
- fn new(name: &str, data: T) -> Self {
- Self {
- name: name.to_owned(),
- data: data.into(),
- _phantom: PhantomData::default(),
- }
- }
- fn set(&mut self, data: T) {
- self.data = data.into();
- }
- }
- fn main() {
- let mut attribute: Attribute<(f32, f32)> = Attribute::new("my_name", (22.2, 22.2));
- attribute.set((6.6, 9.9));
- let mut attribute: Attribute<u32> = Attribute::new("my_name", 42);
- attribute.set(1337);
- }
- impl From<(f32, f32)> for AttributeData {
- fn from(x: (f32, f32)) -> Self {
- AttributeData::FloatPair(x)
- }
- }
- impl From<u32> for AttributeData {
- fn from(x: u32) -> Self {
- AttributeData::Int(x)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement