Skip to content

Commit

Permalink
Implement AsDisplay using GAT
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Sep 2, 2023
1 parent de58088 commit a199524
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
strategy:
fail-fast: false
matrix:
rust: [nightly, beta, stable, 1.56.0]
rust: [nightly, beta, stable, 1.65.0]
timeout-minutes: 45
steps:
- uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ edition = "2021"
keywords = ["error", "error-handling", "derive"]
license = "MIT OR Apache-2.0"
repository = "https://github.com/dtolnay/thiserror"
rust-version = "1.56"
rust-version = "1.65"

[dependencies]
thiserror-impl = { version = "=1.0.47", path = "impl" }
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ This library provides a convenient derive macro for the standard library's
thiserror = "1.0"
```

*Compiler support: requires rustc 1.56+*
*Compiler support: requires rustc 1.65+*

<br>

Expand Down
2 changes: 1 addition & 1 deletion impl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ description = "Implementation detail of the `thiserror` crate"
edition = "2021"
license = "MIT OR Apache-2.0"
repository = "https://github.com/dtolnay/thiserror"
rust-version = "1.56"
rust-version = "1.65"

[lib]
proc-macro = true
Expand Down
30 changes: 17 additions & 13 deletions src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,41 @@ use std::fmt::Display;
use std::path::{self, Path, PathBuf};

#[doc(hidden)]
pub trait AsDisplay<'a> {
type Target: Display;
pub trait AsDisplay {
type Target<'a>: Display
where
Self: 'a;

fn as_display(&'a self) -> Self::Target;
fn as_display<'a>(&'a self) -> Self::Target<'a>;
}

impl<'a, T> AsDisplay<'a> for &T
impl<T> AsDisplay for &T
where
T: Display + 'a,
T: Display,
{
type Target = &'a T;
type Target<'a> = &'a T
where
Self: 'a;

fn as_display(&'a self) -> Self::Target {
fn as_display<'a>(&'a self) -> Self::Target<'a> {
*self
}
}

impl<'a> AsDisplay<'a> for Path {
type Target = path::Display<'a>;
impl AsDisplay for Path {
type Target<'a> = path::Display<'a>;

#[inline]
fn as_display(&'a self) -> Self::Target {
fn as_display<'a>(&'a self) -> Self::Target<'a> {
self.display()
}
}

impl<'a> AsDisplay<'a> for PathBuf {
type Target = path::Display<'a>;
impl AsDisplay for PathBuf {
type Target<'a> = path::Display<'a>;

#[inline]
fn as_display(&'a self) -> Self::Target {
fn as_display<'a>(&'a self) -> Self::Target<'a> {
self.display()
}
}
2 changes: 1 addition & 1 deletion tests/ui/no-display.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ error[E0599]: the method `as_display` exists for reference `&NoDisplay`, but its
|
= note: the following trait bounds were not satisfied:
`NoDisplay: std::fmt::Display`
which is required by `&NoDisplay: AsDisplay<'_>`
which is required by `&NoDisplay: AsDisplay`
note: the trait `std::fmt::Display` must be implemented
--> $RUST/core/src/fmt/mod.rs
|
Expand Down

0 comments on commit a199524

Please sign in to comment.