Feature request: Stabilize `NumBufferTrait` to use as a bound

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.

You're implementing your trait with a macro, right? You can just have the buffer as an associated type.

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 :frowning:

I do the same thing in num-primitive:

1 Like

Actually if NumBuffer implemented Default I wouldn't need the extra trait. It also seem like it should implement Default anyways.

3 Likes

I expect that a PR to implement Default would be simple and uncontroversial.

4 Likes
2 Likes