Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#![warn(bare_trait_objects)]
- pub trait Dynamic {
- fn aaa(&mut self);
- }
- pub trait Callback {
- fn id(&self) -> &'static str;
- }
- #[macro_export]
- macro_rules! callback {
- ($id: ident, $($typ:tt)+) => {
- pub struct $id(Box<$($typ)+>);
- impl Callback for $id {
- fn id(&self) -> &'static str {
- stringify!($id)
- }
- }
- impl <T> From<T> for $id where T: $($typ)+ + Sized + 'static {
- fn from(t: T) -> $id {
- $id(Box::new(t))
- }
- }
- impl AsRef<$($typ)+> for $id {
- //fn as_ref(&self) -> & ($($typ)+ + 'static) { //this compiles (no dyn)
- fn as_ref(&self) -> &dyn ($($typ)+ + 'static) { //this fails (dyn)
- self.0.as_ref()
- }
- }
- impl AsMut<$($typ)+> for $id {
- //fn as_mut(&mut self) -> &mut ($($typ)+ + 'static) { //same here
- fn as_mut(&mut self) -> &mut dyn ($($typ)+ + 'static) {
- self.0.as_mut()
- }
- }
- }
- }
- callback!(Resize, FnMut(&mut dyn Dynamic, u16, u16));
- callback!(Click, FnMut(&mut dyn Dynamic));
- fn main() {}
Add Comment
Please, Sign In to add comment