-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Update to the 2018 edition of Rust #501
Conversation
cc @kballard |
```rust | ||
// in main.rs | ||
|
||
extern crate bootloader; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh excellent, I forgot Rust 2018 let you do this!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it's really cool that we can remove all this boilerplate from the blog.
|
||
First line just prints a `\n` symbol which literally means "don't print anything, just break the line". | ||
Last two rules simply append a newline character (`\n`) to the format string and then invoke the [`print!` macro], which is defined as: | ||
Macros are defined through one or more rules, which are similar to `match` arms. The `println` macro has two rules: The first rule for is invocations without arguments (e.g `println!()`), which is expanded to `print!("\n)` and thus just prints a newline. the second rule is for invocations with parameters such as `println!("Hello")` or `println!("Number: {}", 4)`. It is also expanded to an invokation of the `print!` macro, passing all arguments and an additional newline `\n` at the end. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo: "invokation"
($fmt:expr) => (print!(concat!($fmt, "\n"))); | ||
($fmt:expr, $($arg:tt)*) => (print!(concat!($fmt, "\n"), $($arg)*)); | ||
() => ($crate::print!("\n")); | ||
($($arg:tt)*) => ($crate::print!("{}\n", format_args!($($arg)*))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't realize the output of format_args!()
could be printed like this, but it seems it can. Very cool. 👍
I realize you quoted this trick above as the implementation of std::println!()
, except it isn't really, std::println!()
calls into $crate::io::_print
directly, but what you have here actually seems more straightforward so I can see why you're doing it this way (given that std::println!()
depends on a private format_args_nl!()
compiler builtin).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I decided against the current std::println
implementation because of the internal format_args_nl
macro. This is an older implementation from ~4 months ago: https://github.com/rust-lang/rust/blob/a47653214f8f8561196acf25b8898e7148f1c052/src/libstd/macros.rs#L156-L159
I thought about mentioning that we use an older version and why, but given that some content of the blog is >4 months old anyway this seemed like unnecessary information.
use core::fmt::Write; | ||
SERIAL1.lock().write_fmt(args).expect("Printing to serial failed"); | ||
} | ||
|
||
/// Prints to the host through the serial interface. | ||
#[macro_export] | ||
macro_rules! serial_print { | ||
($($arg:tt)*) => { | ||
$crate::serial::print(format_args!($($arg)*)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line needs to be updated to call _print
.
} | ||
|
||
pub fn print(args: fmt::Arguments) { | ||
pub fn _print(args: fmt::Arguments) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add #[doc(hidden)]
like std::io::_print
uses? I don't know if you want to bring up the whole question of documentation though. I feel like you can put #[doc(hidden)]
on here without explaining it, as your readers can look that up themselves if they want to.
@@ -103,23 +97,25 @@ Like with the [VGA text buffer][vga lazy-static], we use `lazy_static` and a spi | |||
To make the serial port easily usable, we add `serial_print!` and `serial_println!` macros: | |||
|
|||
```rust | |||
pub fn print(args: ::core::fmt::Arguments) { | |||
pub fn _print(args: ::core::fmt::Arguments) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you add #[doc(hidden)]
to vga_buffer::_print
then you'll want to add it here too.
@kballard Thanks a lot for the review! |
bors r+ |
501: Update to the 2018 edition of Rust r=phil-opp a=phil-opp This updates the blog to use the upcoming [2018 edition](https://rust-lang-nursery.github.io/edition-guide/rust-2018/index.html) of Rust, which is currently in beta and already the default on nightly. It changes a few local import paths (now with a `crate::` prefix) and removes all `extern crate` definitions and `macro_use` attributes. This PR changes a lot of code across all posts, so there might be some things in the posts that I forgot to update. Please let me know if you see anything! [Preview of the changes](https://rust-2018--blog-os.netlify.com/) Fixes #499 Fixes #500 Co-authored-by: Philipp Oppermann <dev@phil-opp.com>
Build succeeded |
This updates the blog to use the upcoming 2018 edition of Rust, which is currently in beta and already the default on nightly. It changes a few local import paths (now with a
crate::
prefix) and removes allextern crate
definitions andmacro_use
attributes.This PR changes a lot of code across all posts, so there might be some things in the posts that I forgot to update. Please let me know if you see anything! Preview of the changes
Fixes #499
Fixes #500