# Help with trait method magic in generic bounds

**URL:** <https://users.rust-lang.org/t/help-with-trait-method-magic-in-generic-bounds/141125>\
**Category:** help\
**Created:** [July 7, 2026, 9:35am UTC](https://users.rust-lang.org/t/help-with-trait-method-magic-in-generic-bounds/141125 "2026-07-07T09:35:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Schard](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/schard/32/43802_2.png) [@Schard](https://users.rust-lang.org/u/Schard)\
**Post date:** [July 7, 2026, 9:35am UTC](https://users.rust-lang.org/t/help-with-trait-method-magic-in-generic-bounds/141125/1 "2026-07-07T09:35:30Z")

</div>

I am not sure whether it is even possible, but I currently have this helper function on a type:

```rust
    fn format<T, U>(&self, f: &mut Formatter<'_>, node_id_f: T, ieee_address_f: U) -> fmt::Result
    where
        T: FnOnce(&u16, &mut Formatter<'_>) -> fmt::Result,
        U: FnOnce(&IeeeAddress, &mut Formatter<'_>) -> fmt::Result,
    {
        node_id_f(&self.node_id, f)?;
        write!(f, " (")?;

        if let Some(ieee_address) = self.ieee_address {
            ieee_address_f(&ieee_address, f)?;
        } else {
            write!(f, "N/A")?;
        }

        f.write_str(")")
    }

```

So that I can do this:

```rust
impl Display for Source {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        self.format(f, Display::fmt, Display::fmt)
    }
}

impl LowerHex for Source {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        self.format(f, LowerHex::fmt, Display::fmt)
    }
}

impl UpperHex for Source {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        self.format(f, UpperHex::fmt, UpperHex::fmt)
    }
}

```

Can I specify in the helper function a trait bound for the function, so that I can simplify the trait impls to:

```rust
impl Display for Source {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        self.format(f, Display::fmt)
    }
}

impl LowerHex for Source {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        self.format(f, LowerHex::fmt)
    }
}

impl UpperHex for Source {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        self.format(f, UpperHex::fmt)
    }
}

```

(I guess not, since the trait method probably resolves to a concrete implementation for a type).

EDIT: Fun fact: The bug in the `LowerHex` impl, which I just noticed, is the exact reason for why I want that. 😃

---

<div class="post-metadata">

**Author:** ![jofas](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/jofas/32/30690_2.png) [@jofas](https://users.rust-lang.org/u/jofas)\
**Post date:** [July 7, 2026, 10:13am UTC](https://users.rust-lang.org/t/help-with-trait-method-magic-in-generic-bounds/141125/2 "2026-07-07T10:13:41Z")

</div>

I think this would require some sort of higher-kinded polymorphism the type system currently doesn't support. Instead, to avoid typos like in the snippet, you could use a macro instead of implementing format traits by hand:

```rust
macro_rules! impl_fmt_for_source {
    ($($tr:path),+ $(,)?) => {
        $(
            impl $tr for Source {
                fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
                    self.format(f, <u16 as $tr>::fmt, <IeeeAddress as $tr>::fmt)
                }
            } 
        )+
    }
}

impl_fmt_for_source! { Display, UpperHex, LowerHex }

```

[Playground.](https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=b647470372ecd6984d219194a55cdd11)

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [October 5, 2026, 10:14am UTC](https://users.rust-lang.org/t/help-with-trait-method-magic-in-generic-bounds/141125/3 "2026-10-05T10:14:39Z")

</div>

Closed on October 5, 2026, 10:14am UTC

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.
