Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::rc::*;
- use std::borrow::Borrow;
- use std::convert::AsRef;
- use std::convert::Into;
- use std::convert::From;
- mod cratez {
- use std::rc::*;
- use std::borrow::Borrow;
- use std::convert::AsRef;
- use std::convert::Into;
- use std::convert::From;
- mod private {
- use std::rc::*;
- use std::borrow::Borrow;
- use std::convert::AsRef;
- use std::convert::Into;
- use std::convert::From;
- use crate::cratez::Delegator;
- #[derive(Debug)]
- pub struct IntoDynDelegate {
- pub(super) field: Rc<dyn Delegator>
- }
- impl<D> From<Rc<D>> for IntoDynDelegate where D: Delegator + 'static {
- fn from(value: Rc<D>) -> Self {
- println!("Converting RC");
- Self {field: value }
- }
- }
- impl<D> From<D> for IntoDynDelegate where D: Delegator + 'static {
- fn from(value: D) -> Self {
- println!("Converting Value");
- Rc::new(value).into()
- }
- }
- }
- use crate::cratez::private::IntoDynDelegate;
- #[derive(Debug)]
- pub struct Owner {
- delegate: IntoDynDelegate
- }
- impl Owner {
- pub fn new<D>(delegate: D) -> Self where D: Into<IntoDynDelegate> {
- Owner {
- delegate: delegate.into()
- }
- }
- pub fn delegate(&self) {
- self.delegate.field.hello();
- }
- }
- pub trait Delegator: std::fmt::Debug {
- fn hello(&self) {
- println!("Hello World DEFAULT IMPLEMENTATIN");
- }
- }
- }
- use crate::cratez::Delegator;
- #[derive(Debug)]
- pub struct Delegate {
- field: i32,
- }
- impl Delegate {
- pub fn new(field: i32) -> Self { Delegate { field } }
- }
- impl Delegator for Delegate {
- fn hello(&self) {
- println!("Hello Delegate IMPLEMENTATION");
- }
- }
- fn main() {
- use cratez::{Owner};
- let delegate = Delegate::new(5);
- let owner = Owner::new(delegate);
- owner.delegate();
- let delegate = Rc::new(Delegate::new(99));
- let owner = Owner::new(delegate);
- owner.delegate();
- //println!("{:#?}", owner);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement