Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #[derive(Debug)]
- struct InnerError;
- #[derive(Debug)]
- struct Error<T, U> {
- value1: T,
- value2: U,
- inner: InnerError,
- }
- #[derive(Debug)]
- struct ContextSelector<T0, U0> {
- value1: T0,
- value2: U0,
- }
- // Ideally, this would just be `trait IntoError`, but that causes
- // "the type parameter `T` is not constrained", etc. This generic
- // parameter allows us to "bundle" a bunch of other generics when
- // we implement the trait.
- trait IntoError<T> {
- type From;
- type Error;
- fn into_error(self, inner: Self::From) -> Self::Error;
- }
- impl<T, U, T0, U0> IntoError<(T, U)> for ContextSelector<T0, U0>
- where
- T0: Into<T>,
- U0: Into<U>,
- {
- type From = InnerError;
- type Error = Error<T, U>;
- fn into_error(self, inner: Self::From) -> Self::Error {
- let ContextSelector { value1, value2 } = self;
- let value1 = value1.into();
- let value2 = value2.into();
- Error {
- value1,
- value2,
- inner,
- }
- }
- }
- fn example_i32() -> Error<i32, i32> {
- ContextSelector {
- value1: 1,
- value2: 2,
- }
- .into_error(InnerError)
- }
- fn example_string() -> Error<String, String> {
- ContextSelector {
- value1: "1",
- value2: "2",
- }
- .into_error(InnerError)
- }
- fn main() {
- let a = example_i32();
- let b = example_string();
- println!("{:?}", a);
- println!("{:?}", b);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement