]> git.lizzy.rs Git - rust.git/commit
Implement a format_args!() macro
authorAlex Crichton <alex@alexcrichton.com>
Wed, 28 Aug 2013 09:22:45 +0000 (02:22 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Thu, 12 Sep 2013 07:36:54 +0000 (00:36 -0700)
commit9a5f95a82c2836fa6c57ede49d27c060f2377fa1
treea6548120a917b0a1eaa9afe5cb9595e5c96c377c
parent62166611e7510b86f395dbf19973e442fd43c403
Implement a format_args!() macro

The purpose of this macro is to further reduce the number of allocations which
occur when dealing with formatting strings. This macro will perform all of the
static analysis necessary to validate that a format string is safe, and then it
will wrap up the "format string" into an opaque struct which can then be passed
around.

Two safe functions are added (write/format) which take this opaque argument
structure, unwrap it, and then call the unsafe version of write/format (in an
unsafe block). Other than these two functions, it is not intended for anyone to
ever look inside this opaque struct.

The macro looks a bit odd, but mostly because of rvalue lifetimes this is the
only way for it to be safe that I know of.

Example use-cases of this are:

* third-party libraries can use the default formatting syntax without any
  forced allocations
* the fail!() macro can avoid allocating the format string
* the logging macros can avoid allocation any strings
src/libstd/fmt/mod.rs
src/libsyntax/ext/base.rs
src/libsyntax/ext/ifmt.rs
src/test/compile-fail/ifmt-bad-format-args.rs [new file with mode: 0644]
src/test/compile-fail/ifmt-bad-format-args2.rs [new file with mode: 0644]
src/test/run-pass/ifmt.rs