1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
use information::GeneralInformationAssociator; use information::InformationOrganizer; use information::GeneralInformationProvider; use std::any::Any; use std::mem; pub trait GeneralGraphInformation: GeneralInformationAssociator + InformationOrganizer + GeneralInformationProvider { } impl <T> GeneralGraphInformation for T where T: GeneralInformationAssociator + InformationOrganizer + GeneralInformationProvider { } impl GeneralGraphInformation { pub fn add_information<TInformation>(&mut self, vertex_id: usize, information: TInformation) where TInformation: Any { let self_as_base = unsafe { mem::transmute::<&mut GeneralGraphInformation, &mut GeneralInformationAssociator>(self) }; let mut information_option = Some(information); self_as_base.add_information(vertex_id, &mut information_option); } pub fn get_all_information<TInformation>(&self) -> Vec<&TInformation> where TInformation: Any { let self_as_base = unsafe { mem::transmute::<&GeneralGraphInformation, &GeneralInformationProvider>(self) }; self_as_base .get_all_information() .into_iter() .map( |information| { let result = information.downcast_ref::<TInformation>(); result.expect("You attempted to cast something from an information provider into an invalid type. Aborting.") } ) .collect() } pub fn get_all_information_mut<'a, TInformation>(&'a mut self) -> Vec<&'a mut TInformation> where TInformation: Any { let self_as_base = unsafe { mem::transmute::<&mut GeneralGraphInformation, &mut GeneralInformationProvider>(self) }; self_as_base .get_all_information_mut() .into_iter() .map( |information| { let result = information.downcast_mut::<TInformation>(); result.expect("You attempted to cast something from an information provider into an invalid type. Aborting.") } ) .collect() } pub fn get_information<TInformation>(&self, vertex_id: usize) -> &TInformation where TInformation: Any { let self_as_base = unsafe { mem::transmute::<&GeneralGraphInformation, &GeneralInformationProvider>(self) }; let result = self_as_base.get_information(vertex_id).downcast_ref::<TInformation>(); result.expect("You attempted to cast something from an information provider into an invalid type. Aborting.") } pub fn get_information_mut<'a, TInformation>(&'a mut self, vertex_id: usize) -> &'a mut TInformation where TInformation: Any { let self_as_base = unsafe { mem::transmute::<&mut GeneralGraphInformation, &mut GeneralInformationProvider>(self) }; let result = self_as_base.get_information_mut(vertex_id).downcast_mut::<TInformation>(); result.expect("You attempted to cast something from an information provider into an invalid type. Aborting.") } }