1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-07-13 17:44:20 +02:00

dprint fmt

This commit is contained in:
Andrew Walbran
2025-04-15 13:37:01 +01:00
parent 4f94a87681
commit e56715dd17
21 changed files with 26 additions and 54 deletions

View File

@ -15,9 +15,9 @@
// ANCHOR: hello // ANCHOR: hello
//! Rust <-> Java FFI demo. //! Rust <-> Java FFI demo.
use jni::JNIEnv;
use jni::objects::{JClass, JString}; use jni::objects::{JClass, JString};
use jni::sys::jstring; use jni::sys::jstring;
use jni::JNIEnv;
/// HelloWorld::hello method implementation. /// HelloWorld::hello method implementation.
// SAFETY: There is no other global function of this name. // SAFETY: There is no other global function of this name.

View File

@ -14,8 +14,8 @@
// ANCHOR: exceptions // ANCHOR: exceptions
use log::error; use log::error;
use smccc::psci::system_off;
use smccc::Hvc; use smccc::Hvc;
use smccc::psci::system_off;
// SAFETY: There is no other global function of this name. // SAFETY: There is no other global function of this name.
#[unsafe(no_mangle)] #[unsafe(no_mangle)]

View File

@ -23,8 +23,8 @@ use crate::pl011::Uart;
use core::fmt::Write; use core::fmt::Write;
use core::panic::PanicInfo; use core::panic::PanicInfo;
use log::error; use log::error;
use smccc::psci::system_off;
use smccc::Hvc; use smccc::Hvc;
use smccc::psci::system_off;
/// Base address of the primary PL011 UART. /// Base address of the primary PL011 UART.
const PL011_BASE_ADDRESS: *mut u32 = 0x900_0000 as _; const PL011_BASE_ADDRESS: *mut u32 = 0x900_0000 as _;

View File

@ -22,9 +22,9 @@ mod pl011;
use crate::pl011::Uart; use crate::pl011::Uart;
use core::panic::PanicInfo; use core::panic::PanicInfo;
use log::{error, info, LevelFilter}; use log::{LevelFilter, error, info};
use smccc::psci::system_off;
use smccc::Hvc; use smccc::Hvc;
use smccc::psci::system_off;
/// Base address of the primary PL011 UART. /// Base address of the primary PL011 UART.
const PL011_BASE_ADDRESS: *mut u32 = 0x900_0000 as _; const PL011_BASE_ADDRESS: *mut u32 = 0x900_0000 as _;

View File

@ -23,8 +23,8 @@ use crate::pl011_minimal::Uart;
use core::fmt::Write; use core::fmt::Write;
use core::panic::PanicInfo; use core::panic::PanicInfo;
use log::error; use log::error;
use smccc::psci::system_off;
use smccc::Hvc; use smccc::Hvc;
use smccc::psci::system_off;
/// Base address of the primary PL011 UART. /// Base address of the primary PL011 UART.
const PL011_BASE_ADDRESS: *mut u8 = 0x900_0000 as _; const PL011_BASE_ADDRESS: *mut u8 = 0x900_0000 as _;

View File

@ -20,7 +20,7 @@ extern crate panic_halt as _;
use cortex_m_rt::entry; use cortex_m_rt::entry;
use embedded_hal::digital::OutputPin; use embedded_hal::digital::OutputPin;
use nrf52833_hal::gpio::{p0, Level}; use nrf52833_hal::gpio::{Level, p0};
use nrf52833_hal::pac::Peripherals; use nrf52833_hal::pac::Peripherals;
#[entry] #[entry]

View File

@ -8,7 +8,7 @@ many elements are used and panics if you try to use more than are allocated.
<!-- mdbook-xgettext: skip --> <!-- mdbook-xgettext: skip -->
```rust,editable,compile_fail ```rust,editable,compile_fail
use tinyvec::{array_vec, ArrayVec}; use tinyvec::{ArrayVec, array_vec};
fn main() { fn main() {
let mut numbers: ArrayVec<[u32; 5]> = array_vec!(42, 66); let mut numbers: ArrayVec<[u32; 5]> = array_vec!(42, 66);

View File

@ -10,11 +10,7 @@ A closure can capture variables from the environment where it was defined.
fn main() { fn main() {
let max_value = 5; let max_value = 5;
let clamp = |v| { let clamp = |v| {
if v > max_value { if v > max_value { max_value } else { v }
max_value
} else {
v
}
}; };
dbg!(clamp(1)); dbg!(clamp(1));

View File

@ -17,7 +17,7 @@ the resulting variables. The `statement` result becomes the result of the
```rust,editable,compile_fail ```rust,editable,compile_fail
use tokio::sync::mpsc; use tokio::sync::mpsc;
use tokio::time::{sleep, Duration}; use tokio::time::{Duration, sleep};
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {

View File

@ -15,7 +15,7 @@
// ANCHOR: solution // ANCHOR: solution
// ANCHOR: Philosopher // ANCHOR: Philosopher
use std::sync::Arc; use std::sync::Arc;
use tokio::sync::{mpsc, Mutex}; use tokio::sync::{Mutex, mpsc};
use tokio::time; use tokio::time;
struct Chopstick; struct Chopstick;

View File

@ -22,7 +22,7 @@ with some caveats:
```rust,editable,compile_fail ```rust,editable,compile_fail
use async_trait::async_trait; use async_trait::async_trait;
use std::time::Instant; use std::time::Instant;
use tokio::time::{sleep, Duration}; use tokio::time::{Duration, sleep};
#[async_trait] #[async_trait]
trait Sleeper { trait Sleeper {

View File

@ -20,7 +20,7 @@ location.
```rust,editable,compile_fail ```rust,editable,compile_fail
use tokio::sync::{mpsc, oneshot}; use tokio::sync::{mpsc, oneshot};
use tokio::task::spawn; use tokio::task::spawn;
use tokio::time::{sleep, Duration}; use tokio::time::{Duration, sleep};
// A work item. In this case, just sleep for the given time and respond // A work item. In this case, just sleep for the given time and respond
// with a message on the `respond_on` channel. // with a message on the `respond_on` channel.

View File

@ -8,11 +8,7 @@ minutes: 3
```rust,editable ```rust,editable
fn gcd(a: u32, b: u32) -> u32 { fn gcd(a: u32, b: u32) -> u32 {
if b > 0 { if b > 0 { gcd(b, a % b) } else { a }
gcd(b, a % b)
} else {
a
}
} }
fn main() { fn main() {

View File

@ -15,7 +15,7 @@ writing out trait impls explicitly for custom error types.
[`thiserror`]: https://docs.rs/thiserror/ [`thiserror`]: https://docs.rs/thiserror/
```rust,editable,compile_fail ```rust,editable,compile_fail
use anyhow::{bail, Context, Result}; use anyhow::{Context, Result, bail};
use std::fs; use std::fs;
use std::io::Read; use std::io::Read;
use thiserror::Error; use thiserror::Error;

View File

@ -26,12 +26,12 @@ use embedded_hal::digital::InputPin;
use lsm303agr::{ use lsm303agr::{
AccelMode, AccelOutputDataRate, Lsm303agr, MagMode, MagOutputDataRate, AccelMode, AccelOutputDataRate, Lsm303agr, MagMode, MagOutputDataRate,
}; };
use microbit::Board;
use microbit::display::blocking::Display; use microbit::display::blocking::Display;
use microbit::hal::twim::Twim; use microbit::hal::twim::Twim;
use microbit::hal::uarte::{Baudrate, Parity, Uarte}; use microbit::hal::uarte::{Baudrate, Parity, Uarte};
use microbit::hal::{Delay, Timer}; use microbit::hal::{Delay, Timer};
use microbit::pac::twim0::frequency::FREQUENCY_A; use microbit::pac::twim0::frequency::FREQUENCY_A;
use microbit::Board;
const COMPASS_SCALE: i32 = 30000; const COMPASS_SCALE: i32 = 30000;
const ACCELEROMETER_SCALE: i32 = 700; const ACCELEROMETER_SCALE: i32 = 700;

View File

@ -14,8 +14,8 @@
use arm_gic::gicv3::GicV3; use arm_gic::gicv3::GicV3;
use log::{error, info, trace}; use log::{error, info, trace};
use smccc::psci::system_off;
use smccc::Hvc; use smccc::Hvc;
use smccc::psci::system_off;
// SAFETY: There is no other global function of this name. // SAFETY: There is no other global function of this name.
#[unsafe(no_mangle)] #[unsafe(no_mangle)]

View File

@ -24,16 +24,16 @@ mod pl011;
mod pl031; mod pl031;
use crate::pl031::Rtc; use crate::pl031::Rtc;
use arm_gic::{irq_enable, wfi, IntId, Trigger}; use arm_gic::{IntId, Trigger, irq_enable, wfi};
use chrono::{TimeZone, Utc}; use chrono::{TimeZone, Utc};
use core::hint::spin_loop; use core::hint::spin_loop;
// ANCHOR: imports // ANCHOR: imports
use crate::pl011::Uart; use crate::pl011::Uart;
use arm_gic::gicv3::GicV3; use arm_gic::gicv3::GicV3;
use core::panic::PanicInfo; use core::panic::PanicInfo;
use log::{error, info, trace, LevelFilter}; use log::{LevelFilter, error, info, trace};
use smccc::psci::system_off;
use smccc::Hvc; use smccc::Hvc;
use smccc::psci::system_off;
/// Base addresses of the GICv3. /// Base addresses of the GICv3.
const GICD_BASE_ADDRESS: *mut u64 = 0x800_0000 as _; const GICD_BASE_ADDRESS: *mut u64 = 0x800_0000 as _;

View File

@ -9,11 +9,7 @@ Rust supports generics, which lets you abstract algorithms or data structures
```rust,editable ```rust,editable
fn pick<T>(cond: bool, left: T, right: T) -> T { fn pick<T>(cond: bool, left: T, right: T) -> T {
if cond { if cond { left } else { right }
left
} else {
right
}
} }
fn main() { fn main() {
@ -31,19 +27,11 @@ fn main() {
```rust ```rust
fn pick_i32(cond: bool, left: i32, right: i32) -> i32 { fn pick_i32(cond: bool, left: i32, right: i32) -> i32 {
if cond { if cond { left } else { right }
left
} else {
right
}
} }
fn pick_char(cond: bool, left: char, right: char) -> char { fn pick_char(cond: bool, left: char, right: char) -> char {
if cond { if cond { left } else { right }
left
} else {
right
}
} }
``` ```

View File

@ -29,11 +29,7 @@ job.
struct Point(i32, i32); struct Point(i32, i32);
fn left_most(p1: &Point, p2: &Point) -> &Point { fn left_most(p1: &Point, p2: &Point) -> &Point {
if p1.0 < p2.0 { if p1.0 < p2.0 { p1 } else { p2 }
p1
} else {
p2
}
} }
fn main() { fn main() {

View File

@ -14,11 +14,7 @@ const DIGEST_SIZE: usize = 3;
const FILL_VALUE: u8 = calculate_fill_value(); const FILL_VALUE: u8 = calculate_fill_value();
const fn calculate_fill_value() -> u8 { const fn calculate_fill_value() -> u8 {
if DIGEST_SIZE < 10 { if DIGEST_SIZE < 10 { 42 } else { 13 }
42
} else {
13
}
} }
fn compute_digest(text: &str) -> [u8; DIGEST_SIZE] { fn compute_digest(text: &str) -> [u8; DIGEST_SIZE] {

View File

@ -19,7 +19,7 @@
//! `cargo xtask install-tools` and the logic defined here will install //! `cargo xtask install-tools` and the logic defined here will install
//! the tools. //! the tools.
use anyhow::{anyhow, Ok, Result}; use anyhow::{Ok, Result, anyhow};
use clap::Parser; use clap::Parser;
use std::env; use std::env;
use std::process::Command; use std::process::Command;