Rust Types Team
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Help
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    --- title: 2025-11-25 - impl subtrait via supertrait tags: ["T-Types", "minutes"] discussion: https://rust-lang.zulipchat.com/#narrow/channel/326132-t-types.2Fmeetings/topic/2025-11-25.20impl.20subtrait.20via.20supertrait/with/560204338 url: https://hackmd.io/pwSkzuYkStaQK59QfUwaIA --- # 2025-11-25 - impl subtrait via supertrait Example gist: https://gist.github.com/rust-play/0ab82539ad83b447884beff4df22b17f ## Background Amanieu: The `write!` macro in std is a terrible hack. It takes the writer argument and just calls `write_fmt`. It doesn't do any checking. Depends on what's in scope to figure out what to call. We're trying to fix that over edition. Josh: Depending on which trait you use (fmt::Write or io::Write), each has a fixed error. If you want to write into a `String` or `Vec` those don't need an error. We'd like to make those cases infallible. I've added a change to the compiler so that `Result<(), !>` will not trigger `must_use`. We're trying to create a new trait for `write!` that has an associated `Error` type. Josh: This is where types team comes in. We want to add a mechanism so that both `io::Write` and `fmt::Write` traits could use this trait without breaking the universe. This ties into the auto impl mechanism. If I have this subtrait but I don't already explicitly implement this supertrait, use this auto impl of this supertrait. Amanieu: Blanket impl doesn't work because we want to use the explicit implementation. We want to provide a default blanket impl that can be overridden. Josh: We're hoping this doesn't turn off specialization. Jack: You want to switch the `write!` macro too, right? Josh: Yes, we'll add a new `write` and then over an edition switch wmich write macro is in the prelude. Josh: We'd like `fmt::Write` and `io::Write` to say "if you don't have the impl of `WriteFmt` trait, here's an impl you can use". There are 2 features we'd need: (1) auto impl mechanism and (2) for compatibility in order to say what that associated Error type is, we'd love to be able to have associated type to be `type Error = impl Trait` Jack: Is the only difference between these two traits (Write and WriteFmt) the Error type alias? Josh: the diff between `std::fmt::Write` and `WriteFmt` trait is that the new one has the associated Error type and methods that return it. The `io::Write` has a lot of other methods. Jack: You could add an associated type to the existing trait with a default (which would be the existing error type). That's already in the compiler. Making a default type that's an opaque type might be interesting. But it might work. That seems more simple, have you thought about it? Josh: We did think about a possibility of adding backwards-compatibly an associated type to `io::Write` and `fmt::Write`. If we could, that would be great. In general, implement supertrait in subtrait is something we're interested in many other areas. But putting that aside: we *can* (over an edition) break the `write!` macro. But traits are traits across the entire graph of dependencies regardless the edition. We could add a new `io::Write` but we want to be compatible and have a single `io::Write` type. Josh: If we did that, suppose someone wanted to change that and implement io::Write's error into something like Infallible. Anyone can take that result, store it into `io::Result` and that doesn't work here. The reason we wanted it to be a parent trait is that the ??. Amanieu: The specific breaking change we want to avoid: in the std the `Vec<u8>` implements `io::Write`. And `write_str` returns an `io::Error`. We want the `write!` macro to implement on `Vec<u8>` and have that error be infallible. Jack: I still don't see why having the associated error type is breaking. Josh: Every existing user of io::Write has the right to run the associated write function and get the resulting error. Jack: Every case after that would still have that. Josh: Right. But if you add an asssociated type, that doesn't allow you `write_fmt` method to change to return that associated type because that would break the user. We could add a new method. lcnr: If we add associated type to fmt::write trait. What if in older editions `T: Write` gets lowered and in the future edition you remove the default. Amanieu: It's also for method resolution in general. If you have a type currently that implements `fmt_write` if you call ??. It would be a breaking change for a crate that provides implementation for fmt_write. We're trying to do this for std types like Vec and String. Josh: We want to make it so that every user using `io::Write` can call `write_fmt` and get the `io::Error` they expect. But using the new write! macro they get the associated type. The thing we're relying on is: in the broader crates.io impl, if the default impl is an opaque impl `Into<io::Error>` it's not a breaking change. 80% callers would be fine because they `?` or `unwrap` it and the rest would call `into()`. Josh: Going from an opaque type to a concrete type that implements the methods of the opaque type, that shouldn't be a breaking change. Callers can do all the things they could do before. Jack: So the key bit here that you want to change the error type for existing types. Josh: Yes, we want to change the Error type for things that currently implement io::Write without breaking callers on io::Write so it needs to be a new trait. Jack: Does anyone else have clarifying questions? (no) Jack: Looking at the gist. There are 3 different pieces to think about. (1) auto impl write format (2) priority low (3) `type Error = impl Into<Error>` boxy: lcnr: RPITIT exists that has this exact thing Jack: it's not .. boxy: It's not really an impl, it's a trait implementation with a bunch of defaults and adding an automatic empty default. Jack: From the types side this is not that difficult. The desugaring happens at the ?? level. You need to have some way to ensure that if there's another impl that we don't add this. But you can place enough restrictions on the language side where this becomes trivial. It would be interesting but not that bad. If you write `impl Write for _` you get also `impl WriteFmt for _`. Amanieu: It's basically the same as blanket impl except that it doesn't prevent you from adding more on top of it without running into coherence rules. Jack: You could do that. There are other ways too. Jack: My biggest concern is the `priority[low]`. That might not be attainable soon. Also if you start with enough restrictions, it would be backwards-compatible to add in priority low later on. Jack: The only one that's a little weird is `impl Write for &mut W` (https://doc.rust-lang.org/stable/std/fmt/trait.Write.html#impl-Write-for-%26mut+W) Jack: I'd like to see some examples for where you'd like this auto impl not to apply and where would you expect it to apply. When a user writes an impl of `WriteFmt`, when would this auto impl apply and when it wouldn't. Josh: If the user writes their own impl of WriteFmt, this should never apply. IT should only apply when the user doesn't write their own impl of WriteFmt. Josh: We were thinking of this in concrete types. Where you might choose to add an impl of WriteFmt trait later. But doing things like "for everything that implements this implement io::Write and then for everything that implements *that* implement fmt". Josh: If the first step would only be on concrete types or parametrised ?? but not on blanket impl, that would be acceptable. boxy: We do have something similar syntactically with auto traits. Jack: I wanted to delay that discussion. There's too bikesheddy for now. Josh: important: ```rust impl WriteFmt for ConcreteType impl WriteFmt for GenericType<T> ``` Not important right away: ```rust impl<T: Foo> WriteFmt for T ``` Jack: Counterexample ```rust struct UserTy<T, U>; impl<T, U> WriteFmt for UserTy<T, U> {} impl<T> Write for UserTy<T, ()> {} // Does this get generated: impl<T> WriteFmt for UserTy<T, ()> {} ``` lcnr: Looking at your example, the example what would be very hard to support is this: ```rust struct UserTy<T>(T); impl WriteFmt for UserTy<u32> {} impl Write for UserTy<i32> {} // Does this get generated: impl WriteFmt for UserTy<i32> {} ``` Jack: If you make this a syntactic check, then it's okay and on the type side is fine. If you want anything deeper, you'll end up with a lot of issues. I'm happy to do another meeting on this if we prepare a doc ahead of time and properly schedule it out. Josh: Thank you for taking about this and doing it much sooner than we expected. Jack: That's why I wanted to do this soon -- to have the vibe discussion. But if want to go deeper, let's get the design doc going and schedule a proper meeting. lcnr: You've got the auto trait type that's got some edge cases that won't work well. The priority when emitting impls is hard to implement. Josh: That would be really nice if we could have a solution to that. But if you told us that'd be painful to do, we could rely on the fact that nobody in practice implements both `io::Write` and `fmt::Write` on the same type. We'd have to check the ecosystem, but nothing in the std does this. So if we said you can only implement only one, that might not be the end of the world. lcnr: If it happens during lowering without ever doing any type related traits or whatever, that's a bit ugly but it's not doing anything horrible with the type system. That's more of a language question. Josh: This is an experiment where several of us on libs-api and lang, we often end up coming to types where in the course of something else we had to end up with types. But we rarely talk to types earlier. If you see another way of solving this or design constraints to apply to this to make it wildly easier in the type system, give us those constraints and we'll see if we can come up with those constraints. lcnr: If you want to do this in types properly to emit the implementations, that's something we don't do a lot currently. lcnr: If you want to use an overlap check, we need to know all the impls that exist. Josh: Yeah, you're suddenly turn into "you're allowed overlapping impls as long as none of them is auto impl". lcnr: The compiler also needs to prove this guarantee. You could imagine having WriteFmt and io::Write impl which can disagree over the arguments Josh: This is where we'd love to receive any restrictions (e.g. to make this really obvious to the compiler). It would be fine if the only case you could do is some type (possibly with a generic type parameter) implements io::Write Josh: If we said the only way this works is if your impl WriteFmt syntactically matches module what you call these things, then you're effectively saying "if it syntactically matches such that it has textually the same bounds, then we can make it work; and otherwise we can't instantly tell and therefore we can't do it now but might in the future". That would be acceptable. Josh: We'd use it in the standard library at first. And we could let the ecosystem use it under these restrictions. That would still be incredibly valuable. Most people want the `ConcreteType` or `SimpleGenericType<T>` lcnr: That one feels easy enough. It doesn't touch the type system. Josh: If you can spell out which restrictions makes it easy of you. lcnr: The type constructur must be a rigid type (no type parameters) that's unique Josh: Right, you can't make this to be a GAT (generic associated type). lcnr: You need a type that's only equal to itself assuming the generic parameters match. Josh: If the restriction is "don't let this touch GATs", that's fine lcnr: Doesn't have to be a GAT, it can also be `<T as Trait>::Assoc`. Josh: I can live with that for now, but I'd love to know where on the "this is hard" scale is. Josh: Thank you everyone.

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully