Skip to content

Instantly share code, notes, and snippets.

@cstorey
Created August 12, 2016 10:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cstorey/533d2c1c947ab156e6c1ba24a81efb1f to your computer and use it in GitHub Desktop.
Save cstorey/533d2c1c947ab156e6c1ba24a81efb1f to your computer and use it in GitHub Desktop.
Enough higher-kinded-ness to (trivially) abstract over pointer types in rust.
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