tyilo
September 8, 2026, 3:20pm
1
I would like to add the format_into function to my Integer trait.
This doesn't seem to be possible because of the T: NumBufferTrait bound in the definition of NumBuffer which requires me to add NumBufferTrait as a super bound to Integer.
If NumBufferTrait could be used as a bound, this would allow me to implement this function.
A similar issue exists with the to_int_unchecked function and the FloatToInt trait.
chrefr
September 8, 2026, 3:47pm
2
You're implementing your trait with a macro, right? You can just have the buffer as an associated type.
tyilo
September 8, 2026, 4:19pm
3
Yeah, you're right that I could do something like:
use core::fmt::NumBuffer;
trait MyNumBuffer {
fn new() -> Self;
}
trait Integer {
type NumBuffer: MyNumBuffer;
fn format_into(self, buf: &mut Self::NumBuffer) -> &str;
}
impl MyNumBuffer for NumBuffer<u32> {
fn new() -> Self {
NumBuffer::new()
}
}
impl Integer for u32 {
type NumBuffer = NumBuffer<Self>;
fn format_into(self, buf: &mut Self::NumBuffer) -> &str {
Self::format_into(self, buf)
}
}
fn print_int<T: Integer>(value: T) {
let mut buf = T::NumBuffer::new();
let formatted = value.format_into(&mut buf);
println!("{}", formatted);
}
fn main() {
print_int(1234);
}
However that requires defining my own MyNumBuffer trait
cuviper
September 8, 2026, 5:21pm
4
I do the same thing in num-primitive:
1 Like
tyilo
September 8, 2026, 8:19pm
5
Actually if NumBuffer implemented Default I wouldn't need the extra trait. It also seem like it should implement Default anyways.
3 Likes
kpreid
September 8, 2026, 8:55pm
6
I expect that a PR to implement Default would be simple and uncontroversial.
4 Likes