Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

impl Into<T> for Infallible where T: Error #233

Closed
Pr0methean opened this issue Jun 1, 2023 · 3 comments
Closed

impl Into<T> for Infallible where T: Error #233

Pr0methean opened this issue Jun 1, 2023 · 3 comments
Labels
ACP-accepted API Change Proposal is accepted (seconded with no objections) api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api

Comments

@Pr0methean
Copy link

Pr0methean commented Jun 1, 2023

Proposal

Problem statement

Infallible should implicitly convert to all other error types.

Motivating examples or use cases

I wrote the following function:

#[cfg(feature = "time")]
impl TryFrom<OffsetDateTime> for DateTime {
    type Error = DateTimeRangeError;

    fn try_from(dt: OffsetDateTime) -> Result<Self, Self::Error> {
        if dt.year() >= 1980 && dt.year() <= 2107 {
            Ok(DateTime {
                year: (dt.year()).try_into()?,
                month: (dt.month()).try_into()?,
                day: dt.day(),
                hour: dt.hour(),
                minute: dt.minute(),
                second: dt.second(),
            })
        } else {
            Err(DateTimeRangeError)
        }
    }
}

and found it didn't compile until I added this conversion:

impl From<Infallible> for DateTimeRangeError {
    fn from(_value: Infallible) -> Self {
        unreachable!()
    }
}

Solution sketch

impl <T> Into<T> for Infallible where T: Error {
    fn into(self) -> T {
        unsafe {
            unreachable_unchecked()
        }
    }
}

Alternatives

Alternatives would include:

  • a type-checked variant of unwrap() that would only unwrap Infallible.
  • Specializations of all the Try interfaces, such as impl From<U> for T where T: TryFrom<U,Error=Infallible>, but this would mean adding code in a lot of places rather than only one.

What happens now?

This issue is part of the libs-api team API change proposal process. Once this issue is filed the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.

Possible responses

The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):

  • We think this problem seems worth solving, and the standard library might be the right place to solve it.
  • We think that this probably doesn't belong in the standard library.

Second, if there's a concrete solution:

  • We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
  • We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.
@Pr0methean Pr0methean added api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api labels Jun 1, 2023
@pitaj
Copy link

pitaj commented Jun 1, 2023

Is this even possible? There are already types which implement From<Infallible> - won't this conflict with those?

@Pr0methean
Copy link
Author

Pr0methean commented Jun 1, 2023

Maybe the blanket implementation could be annotated with a special #[ignore_all_other_impls] flag that was only visible to std::*. I've also added rust-lang/rust#35121 (comment) since we won't have this problem with impl Into<T> for ! if it's added before ! is stabilized.

@Amanieu
Copy link
Member

Amanieu commented Jun 13, 2023

We discussed this in the libs-api meeting today. We're happy to have impl From<T> for Infallible (without the : Error bound) but this is blocked on language support since this would conflict with the blanket impl From<T> for T.

See rust-lang/rust#35121 and rust-lang/rust#64631 for further discussion.

@Amanieu Amanieu closed this as completed Jun 13, 2023
@Amanieu Amanieu added the ACP-accepted API Change Proposal is accepted (seconded with no objections) label Jun 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ACP-accepted API Change Proposal is accepted (seconded with no objections) api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api
Projects
None yet
Development

No branches or pull requests

3 participants