# Could core::fmt::Formatter be an alias to a trait object?

**URL:** <https://internals.rust-lang.org/t/could-core-formatter-be-an-alias-to-a-trait-object/15418>\
**Category:** libs\
**Created:** [October 4, 2021, 6:04pm UTC](https://internals.rust-lang.org/t/could-core-formatter-be-an-alias-to-a-trait-object/15418 "2021-10-04T18:04:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![soruh](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/soruh/32/6385_2.png) [@soruh](https://internals.rust-lang.org/u/soruh)\
**Post date:** [October 4, 2021, 6:04pm UTC](https://internals.rust-lang.org/t/could-core-formatter-be-an-alias-to-a-trait-object/15418/1 "2021-10-04T18:04:28Z")

</div>

As far as I know there is currently no way to use a `Debug` implementation with anything but `core::fmt::Formatter`.

Since `core::fmt::Formatter` is only ever accessible by reference and is neither `Clone` nor `Copy`, would it be possible to change the definition of `core::fmt::Formatter` to `pub type Formatter = dyn FormatterTrait` where `FormatterTrait` implements all methods that `core::fmt::Formatter` currently does?

The standard library could then make the current `core::fmt::Formatter` private and implement `FormatterTrait` for it.

As far as I can see, this would allow custom formatters and wouldn't break any existing code.

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [October 4, 2021, 6:08pm UTC](https://internals.rust-lang.org/t/could-core-formatter-be-an-alias-to-a-trait-object/15418/2 "2021-10-04T18:08:33Z")

</div>

[Unfortunately not; it's rather trivial to observe `Formatter: Sized`](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=5f0de39fbe6c7a27e176c92e5557502b).

---

<div class="post-metadata">

**Author:** ![soruh](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/soruh/32/6385_2.png) [@soruh](https://internals.rust-lang.org/u/soruh)\
**Post date:** [October 4, 2021, 6:15pm UTC](https://internals.rust-lang.org/t/could-core-formatter-be-an-alias-to-a-trait-object/15418/3 "2021-10-04T18:15:25Z")

</div>

Ah, I forgot the docs don't tell you if a type is `Sized`. That's unfortunate. Especially since observing if `Formatter` is `Sized` is such a useless thing to do... I'm still not quite sure what an edition can actually change but would this be something Rust 2024 could change?

---

<div class="post-metadata">

**Author:** ![quinedot](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/quinedot/32/7294_2.png) [@quinedot](https://internals.rust-lang.org/u/quinedot)\
**Post date:** [October 4, 2021, 7:50pm UTC](https://internals.rust-lang.org/t/could-core-formatter-be-an-alias-to-a-trait-object/15418/4 "2021-10-04T19:50:38Z")

</div>

It's more properly a `Formatter<'a>`. Conceivably it could hold a `&'a dyn FormatterTrait`. Or become generic with a default underlying type. I think it would take a lot of motivation though.

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [October 4, 2021, 9:05pm UTC](https://internals.rust-lang.org/t/could-core-formatter-be-an-alias-to-a-trait-object/15418/5 "2021-10-04T21:05:20Z")

</div>

It's worth noting that `Formatter` already effectively _does_ do dynamic dispatch - to some `fmt::Write` or `io::Write`. So if you want to use the `Debug` representation of some type, you implement `fmt::Write` and `write!` to it.

It won't give you structured data (it's not supposed to! the `Formatter` helpers are just helpers over `write!`ing simple data) but it will give you access to the debug formatted stream without requiring a temp string buffer.

---

<div class="post-metadata">

**Author:** ![soruh](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/soruh/32/6385_2.png) [@soruh](https://internals.rust-lang.org/u/soruh)\
**Post date:** [October 4, 2021, 10:03pm UTC](https://internals.rust-lang.org/t/could-core-formatter-be-an-alias-to-a-trait-object/15418/6 "2021-10-04T22:03:17Z")

</div>

Hmm, yeah I guess under the lense of the data being completely unstructured it doesn't make sense.

I was thinking about ways to have colored debug output à la nodejs and this seemed to solve the problem of having to derive a trait for unrelated crates to be able to print them. I guess this is not really sufficient motivation for this change if it isn't trivial.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [January 2, 2022, 10:03pm UTC](https://internals.rust-lang.org/t/could-core-formatter-be-an-alias-to-a-trait-object/15418/7 "2022-01-02T22:03:24Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
