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

Conversation

2 participants
@phil-opp
Owner

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

This comment has been minimized.

Owner

phil-opp commented Nov 18, 2018

```rust
// in main.rs
extern crate bootloader;

This comment has been minimized.

@kballard

kballard Nov 18, 2018

Oh excellent, I forgot Rust 2018 let you do this!

This comment has been minimized.

@phil-opp

phil-opp Nov 19, 2018

Owner

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.

This comment has been minimized.

@kballard

kballard Nov 18, 2018

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

This comment has been minimized.

@kballard

kballard Nov 18, 2018

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

This comment has been minimized.

@phil-opp

phil-opp Nov 19, 2018

Owner

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

This comment has been minimized.

@kballard

kballard Nov 18, 2018

This line needs to be updated to call _print.

}
pub fn print(args: fmt::Arguments) {
pub fn _print(args: fmt::Arguments) {

This comment has been minimized.

@kballard

kballard Nov 18, 2018

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

This comment has been minimized.

@kballard

kballard Nov 18, 2018

If you add #[doc(hidden)] to vga_buffer::_print then you'll want to add it here too.

@phil-opp

This comment has been minimized.

Owner

phil-opp commented Nov 19, 2018

@kballard Thanks a lot for the review!

@phil-opp

This comment has been minimized.

Owner

phil-opp commented Nov 19, 2018

bors r+

bors bot added a commit that referenced this pull request Nov 19, 2018

Merge #501
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

This comment has been minimized.

Contributor

bors bot commented Nov 19, 2018

@bors bors bot merged commit ad7c11c into master Nov 19, 2018

2 of 4 checks passed

continuous-integration/appveyor/pr Waiting for AppVeyor build to complete
Details
continuous-integration/travis-ci/pr The Travis CI build is in progress
Details
bors Build succeeded
Details
deploy/netlify Deploy preview ready!
Details

@bors bors bot deleted the rust-2018 branch Nov 19, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment