Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#fmt should allow any width int, float, etc to be used #1653

Closed
brson opened this issue Jan 24, 2012 · 11 comments
Closed

#fmt should allow any width int, float, etc to be used #1653

brson opened this issue Jan 24, 2012 · 11 comments
Labels
A-syntaxext Area: Syntax extensions C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@brson
Copy link
Contributor

brson commented Jan 24, 2012

It would be nice if "%d" would work for any signed integer type, etc. Doing this would require giving extensions access to type information.

@nikomatsakis
Copy link
Contributor

It would be nice if this worked. I wonder if one way would be to simply promote the argument to u64 or i64 as appropriate?

#fmt["%u", x] would translate to u64::to_str(x as u64, 10) etc.

@nikomatsakis
Copy link
Contributor

(Such promotion is, after all, how printf works for types smaller than int)

@brson
Copy link
Contributor Author

brson commented Jan 24, 2012

Yeah, we could totally do that. I never really considered implementing it like that because it's a silly little price to pay, but it's very easy, and if there's any real performance impact it's probably not important.

@nikomatsakis
Copy link
Contributor

In general it'd be nice to have type information in the extensions.

@brson
Copy link
Contributor Author

brson commented Jan 24, 2012

It would make error messages about incorrect types slightly more opaque (they would be about bad casts to i64, etc.) and would allow for some things that are unexpected, #fmt("%b", 1000) - anything that's castable.

@nikomatsakis
Copy link
Contributor

The point about error msgs is well-taken. As for "%b" (that's boolean, right?), I don't think you need to use as except for the integral types. Still, it'd probably be better to have the actual type info at hand in terms of generating the optimal user experience. But then it'd take 2 seconds to have it working the other way ;)

@brson
Copy link
Contributor Author

brson commented Jan 24, 2012

You're right that %b (bool) wouldn't be affected. Basically, I think doing the cast is ok, but if we really wanted to limit the casts to just the obvious stuff, we could add an iface to do the conversion:

mod std::extfmt {

    iface fmt_int<T> {
        fn to_fmt_int() -> i64;
    }

}

Then #fmt could expand every int argument to { import std::extfmt::*; arg.to_fmt_int() }

@nikomatsakis
Copy link
Contributor

I considered suggesting an iface, but I rejected it because it wouldn't be in scope. I didn't think of using a block to limit the scope like that, clever.

@nikomatsakis
Copy link
Contributor

Thinking about this now, I think that if we were to use an iface, we would probably want to have the core imports bring the impls into scope for the built-in types. This would allow people to override them on a file-by-file basis, as well as allowing user-defined types to work with %d, %s, etc. If the aim is not to allow user-defined types and customizable formatting, then I would advocate just casting to u64,i64, etc---it'll compile faster.

@lilyball
Copy link
Contributor

I'd love to see this happen. I'm getting tired of throwing in casts every time I want to print the hex value of a u8. I'd also accept simply adding support for the printf length modifiers, so %hhx would print a u8 as hex.

@ahnurmi
Copy link

ahnurmi commented Apr 29, 2013

It seems that currently fmt!("%?", u64::max_value) silently truncates to uint. It would be good to get this fixed, too.

flip1995 pushed a commit to flip1995/rust that referenced this issue May 27, 2020
Update 'or_fun_call' to ignore calls to len

Resolves rust-lang#1653

changelog: Update `or_fun_call`: Allow calls to `len` for Slice, Array & Vec.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-syntaxext Area: Syntax extensions C-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

No branches or pull requests

4 participants