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

Update to the 2018 edition of Rust #501

Merged
merged 12 commits into from
Nov 19, 2018
Merged

Update to the 2018 edition of Rust #501

merged 12 commits into from
Nov 19, 2018

Conversation

phil-opp
Copy link
Owner

@phil-opp phil-opp commented Nov 18, 2018

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

Fixes #499
Fixes #500

@phil-opp phil-opp added feedback wanted We want to hear opinions about this change. relnotes "Release notes" – Notable changes that are rendered on the blog. labels Nov 18, 2018
@phil-opp
Copy link
Owner Author

cc @kballard

```rust
// in main.rs

extern crate bootloader;

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!

Copy link
Owner Author

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.

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)*)));

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).

Copy link
Owner Author

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)*));

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) {

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) {

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.

@phil-opp
Copy link
Owner Author

@kballard Thanks a lot for the review!

@phil-opp
Copy link
Owner Author

bors r+

bors bot added a commit that referenced this pull request Nov 19, 2018
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>
@bors
Copy link
Contributor

bors bot commented Nov 19, 2018

Build succeeded

@bors bors bot merged commit ad7c11c into master Nov 19, 2018
@bors bors bot deleted the rust-2018 branch November 19, 2018 09:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feedback wanted We want to hear opinions about this change. relnotes "Release notes" – Notable changes that are rendered on the blog.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Transition the code to Rust 2018 Remove remaining uses of #[macro_use]
2 participants