Created
October 7, 2022 16:04
-
-
Save cdecompilador/5646117a7c8f6bcd709514bfb8bdde2f to your computer and use it in GitHub Desktop.
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::convert::TryFrom; | |
| trait IntoAbstract: TryInto<Abstract> { | |
| fn into_abstract(self) -> Abstract { | |
| self.try_into().ok().unwrap() | |
| } | |
| } | |
| trait FromAbstract: TryFrom<Abstract> { | |
| fn from_abstract(value: Abstract) -> Self { | |
| Self::try_from(value).ok().unwrap() | |
| } | |
| } | |
| impl IntoAbstract for i32 {} | |
| impl FromAbstract for i32 {} | |
| struct Abstract; | |
| impl TryFrom<i32> for Abstract { | |
| type Error = (); | |
| fn try_from(value: i32) -> Result<Self, Self::Error> { | |
| Ok(Abstract) | |
| } | |
| } | |
| impl<T> TryFrom<(T, T)> for Abstract { | |
| type Error = (); | |
| fn try_from(value: (T, T)) -> Result<Self, Self::Error> { | |
| todo!() | |
| } | |
| } | |
| impl TryFrom<Abstract> for i32 { | |
| type Error = (); | |
| fn try_from(value: Abstract) -> Result<Self, Self::Error> { | |
| Ok(0) | |
| } | |
| } | |
| struct Foo; | |
| trait FunCall<I, O> { | |
| fn call(&self, input: I) -> O; | |
| } | |
| impl<I, O> FunCall<I, O> for Foo | |
| where | |
| I: IntoAbstract, | |
| O: FromAbstract | |
| { | |
| fn call(&self, input: I) -> O { | |
| let value = input.try_into().ok().unwrap(); | |
| O::from_abstract(value) | |
| } | |
| } | |
| impl<I1, I2, O> FunCall<(I1, I2), O> for Foo | |
| where | |
| I1: IntoAbstract, | |
| I2: IntoAbstract, | |
| O: FromAbstract | |
| { | |
| fn call(&self, (input1, input2): (I1, I2)) -> O { | |
| let value = input1.into_abstract(); | |
| let value2 = input2.into_abstract(); | |
| O::from_abstract(value) | |
| } | |
| } | |
| fn main() { | |
| let foo = Foo; | |
| let res: i32 = foo.call(10i32); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment