Created
August 26, 2018 17:34
-
-
Save rust-play/2c38d6a6f2f43f5785c42c153d2370b2 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains 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::marker::PhantomData; | |
trait ConnT<T> { | |
type Res; | |
fn proc(T) -> Self::Res; | |
} | |
struct Conn1; | |
impl<T> ConnT<T> for Conn1 { | |
type Res = Result<T, ()>; | |
fn proc(res: T) -> Self::Res { | |
Ok(res) | |
} | |
} | |
struct Conn2; | |
impl<T> ConnT<T> for Conn2 { | |
type Res = Option<T>; | |
fn proc(res: T) -> Self::Res { | |
Some(res) | |
} | |
} | |
struct Pub<Adapt> (PhantomData<Adapt>); | |
impl<C> Pub<C> { | |
pub fn get(&self, req: &str) -> String { | |
println!("{}", req); | |
("1".to_string())+req | |
} | |
} | |
impl<C> Pub<C> | |
{ | |
pub fn check1(&self) -> C::Res | |
where C: ConnT<String> | |
{ | |
C::proc(self.get("1")) | |
} | |
pub fn check2(&self) -> C::Res | |
where C: ConnT<u32> | |
{ | |
C::proc(self.get("1").parse().unwrap()) | |
} | |
} | |
fn main() { | |
let p1:Pub<Conn1> = Pub(PhantomData); | |
let p2:Pub<Conn2> = Pub(PhantomData); | |
let res = p1.check1(); | |
println!("{:?}", res); | |
let res = p1.check2(); | |
println!("{:?}", res); | |
let res = p2.check1(); | |
println!("{:?}", res); | |
let res = p2.check2(); | |
println!("{:?}", res); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment