Created
August 12, 2016 10:55
-
-
Save cstorey/533d2c1c947ab156e6c1ba24a81efb1f to your computer and use it in GitHub Desktop.
Enough higher-kinded-ness to (trivially) abstract over pointer types in rust.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::ops::Deref; | |
use std::rc::Rc; | |
use std::sync::Arc; | |
trait Ptr<T> { | |
type PT : Deref<Target=T>; | |
fn build(val:T) -> Self::PT; | |
} | |
#[derive(Debug)] | |
struct ArcP; | |
#[derive(Debug)] | |
struct RcP; | |
impl<T> Ptr<T> for ArcP { | |
type PT = Arc<T>; | |
fn build(val:T) -> Self::PT { | |
Arc::new(val) | |
} | |
} | |
impl<T> Ptr<T> for RcP { | |
type PT = Rc<T>; | |
fn build(val:T) -> Self::PT { | |
Rc::new(val) | |
} | |
} | |
#[derive(Debug)] | |
struct Thing<P:Ptr<T>, T>{ | |
val: P::PT | |
} | |
impl<P:Ptr<T>, T> Thing<P, T> { | |
fn new(val:T) ->Self { | |
Thing { val: P::build(val) } | |
} | |
} | |
fn main() { | |
let a: Thing<ArcP, u64> = Thing::new(1); | |
let b: Thing<RcP, u64> = Thing::new(2); | |
println!("a:{:?}", a); | |
println!("b:{:?}", b); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment