Skip to content

Commit 74d82e3

Browse files
committed
style: format code blocks
Signed-off-by: simonsan <[email protected]>
1 parent a37b288 commit 74d82e3

18 files changed

+110
-89
lines changed

dprint.json .dprint.json

+8-1
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,17 @@
2424
"theme/book.js",
2525
"target/**/*"
2626
],
27+
"exec": {
28+
"commands": [{
29+
"command": "rustfmt --edition 2021",
30+
"exts": ["rs"]
31+
}]
32+
},
2733
"plugins": [
2834
"https://plugins.dprint.dev/markdown-0.16.4.wasm",
2935
"https://plugins.dprint.dev/toml-0.6.1.wasm",
3036
"https://plugins.dprint.dev/json-0.19.2.wasm",
31-
"https://plugins.dprint.dev/typescript-0.89.3.wasm"
37+
"https://plugins.dprint.dev/typescript-0.89.3.wasm",
38+
"https://plugins.dprint.dev/exec-0.4.4.json@c207bf9b9a4ee1f0ecb75c594f774924baf62e8e53a2ce9d873816a408cecbf7"
3239
]
3340
}

.github/workflows/lint.yml

+9-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,12 @@ jobs:
1111
steps:
1212
- uses: actions/checkout@v4
1313

14-
- uses: dprint/[email protected]
14+
- name: Install Rust toolchain
15+
uses: dtolnay/rust-toolchain@1482605bfc5719782e1267fd0c0cc350fe7646b8 # v1
16+
with:
17+
toolchain: stable
18+
components: rustfmt
19+
20+
- uses: Swatinem/rust-cache@23bce251a8cd2ffc3c1075eaa2367cf899916d84 # v2
21+
22+
- uses: dprint/[email protected]

src/anti_patterns/deny-warnings.md

+28-24
Original file line numberDiff line numberDiff line change
@@ -58,35 +58,39 @@ is a list of warning lints that is (hopefully) safe to deny (as of Rustc
5858
1.48.0):
5959

6060
```rust,ignore
61-
#![deny(bad_style,
62-
const_err,
63-
dead_code,
64-
improper_ctypes,
65-
non_shorthand_field_patterns,
66-
no_mangle_generic_items,
67-
overflowing_literals,
68-
path_statements,
69-
patterns_in_fns_without_body,
70-
private_in_public,
71-
unconditional_recursion,
72-
unused,
73-
unused_allocation,
74-
unused_comparisons,
75-
unused_parens,
76-
while_true)]
61+
#![deny(
62+
bad_style,
63+
const_err,
64+
dead_code,
65+
improper_ctypes,
66+
non_shorthand_field_patterns,
67+
no_mangle_generic_items,
68+
overflowing_literals,
69+
path_statements,
70+
patterns_in_fns_without_body,
71+
private_in_public,
72+
unconditional_recursion,
73+
unused,
74+
unused_allocation,
75+
unused_comparisons,
76+
unused_parens,
77+
while_true
78+
)]
7779
```
7880

7981
In addition, the following `allow`ed lints may be a good idea to `deny`:
8082

8183
```rust,ignore
82-
#![deny(missing_debug_implementations,
83-
missing_docs,
84-
trivial_casts,
85-
trivial_numeric_casts,
86-
unused_extern_crates,
87-
unused_import_braces,
88-
unused_qualifications,
89-
unused_results)]
84+
#![deny(
85+
missing_debug_implementations,
86+
missing_docs,
87+
trivial_casts,
88+
trivial_numeric_casts,
89+
unused_extern_crates,
90+
unused_import_braces,
91+
unused_qualifications,
92+
unused_results
93+
)]
9094
```
9195

9296
Some may also want to add `missing-copy-implementations` to their list.

src/functional/generics-type-classes.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ mod bootp {
120120

121121
// private module, lest outside users invent their own protocol kinds!
122122
mod proto_trait {
123-
use std::path::{Path, PathBuf};
124123
use super::{bootp, nfs};
124+
use std::path::{Path, PathBuf};
125125

126126
pub(crate) trait ProtoKind {
127127
type AuthInfo;
@@ -157,7 +157,7 @@ mod proto_trait {
157157
}
158158

159159
use proto_trait::ProtoKind; // keep internal to prevent impls
160-
pub use proto_trait::{Nfs, Bootp}; // re-export so callers can see them
160+
pub use proto_trait::{Bootp, Nfs}; // re-export so callers can see them
161161

162162
struct FileDownloadRequest<P: ProtoKind> {
163163
file_name: PathBuf,

src/functional/optics.md

+8-5
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,8 @@ use anyhow;
185185
use std::str::FromStr;
186186
187187
struct TestStruct {
188-
a: usize,
189-
b: String,
188+
a: usize,
189+
b: String,
190190
}
191191
192192
impl FromStr for TestStruct {
@@ -203,7 +203,10 @@ impl ToString for TestStruct {
203203
}
204204
205205
fn main() {
206-
let a = TestStruct { a: 5, b: "hello".to_string() };
206+
let a = TestStruct {
207+
a: 5,
208+
b: "hello".to_string(),
209+
};
207210
println!("Our Test Struct as JSON: {}", a.to_string());
208211
}
209212
```
@@ -264,8 +267,8 @@ independent form, it is even possible to have code generation creating the
264267
```rust,ignore
265268
#[derive(Default, Serde)] // the "Serde" derive creates the trait impl block
266269
struct TestStruct {
267-
a: usize,
268-
b: String,
270+
a: usize,
271+
b: String,
269272
}
270273
271274
// user writes this macro to generate an associated visitor type

src/idioms/coercion-arguments.md

+4-5
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ fn three_vowels(word: &String) -> bool {
3636
'a' | 'e' | 'i' | 'o' | 'u' => {
3737
vowel_count += 1;
3838
if vowel_count >= 3 {
39-
return true
39+
return true;
4040
}
4141
}
42-
_ => vowel_count = 0
42+
_ => vowel_count = 0,
4343
}
4444
}
4545
false
@@ -54,7 +54,6 @@ fn main() {
5454
// This works fine, but the following two lines would fail:
5555
// println!("Ferris: {}", three_vowels("Ferris"));
5656
// println!("Curious: {}", three_vowels("Curious"));
57-
5857
}
5958
```
6059

@@ -97,10 +96,10 @@ fn three_vowels(word: &str) -> bool {
9796
'a' | 'e' | 'i' | 'o' | 'u' => {
9897
vowel_count += 1;
9998
if vowel_count >= 3 {
100-
return true
99+
return true;
101100
}
102101
}
103-
_ => vowel_count = 0
102+
_ => vowel_count = 0,
104103
}
105104
}
106105
false

src/idioms/ctor.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ object:
1616
/// assert_eq!(42, s.value());
1717
/// ```
1818
pub struct Second {
19-
value: u64
19+
value: u64,
2020
}
2121

2222
impl Second {
@@ -47,7 +47,7 @@ Rust supports default constructors with the [`Default`][std-default] trait:
4747
/// assert_eq!(0, s.value());
4848
/// ```
4949
pub struct Second {
50-
value: u64
50+
value: u64,
5151
}
5252

5353
impl Second {
@@ -78,7 +78,7 @@ like they do with `Second`:
7878
/// ```
7979
#[derive(Default)]
8080
pub struct Second {
81-
value: u64
81+
value: u64,
8282
}
8383

8484
impl Second {

src/idioms/ffi/accepting-strings.md

+1-4
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,7 @@ pub mod unsafe_module {
4444
/// - points to memory ending in a null byte
4545
/// - won't be mutated for the duration of this function call
4646
#[no_mangle]
47-
pub unsafe extern "C" fn mylib_log(
48-
msg: *const libc::c_char,
49-
level: libc::c_int
50-
) {
47+
pub unsafe extern "C" fn mylib_log(msg: *const libc::c_char, level: libc::c_int) {
5148
let level: crate::LogLevel = match level { /* ... */ };
5249
5350
// SAFETY: The caller has already guaranteed this is okay (see the

src/idioms/ffi/errors.md

+8-9
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ in a usable way:
2020

2121
```rust,ignore
2222
enum DatabaseError {
23-
IsReadOnly = 1, // user attempted a write operation
24-
IOError = 2, // user should read the C errno() for what it was
23+
IsReadOnly = 1, // user attempted a write operation
24+
IOError = 2, // user should read the C errno() for what it was
2525
FileCorrupted = 3, // user should run a repair tool to recover it
2626
}
2727
@@ -57,10 +57,7 @@ pub mod c_api {
5757
use super::errors::DatabaseError;
5858
5959
#[no_mangle]
60-
pub extern "C" fn db_error_description(
61-
e: *const DatabaseError
62-
) -> *mut libc::c_char {
63-
60+
pub extern "C" fn db_error_description(e: *const DatabaseError) -> *mut libc::c_char {
6461
let error: &DatabaseError = unsafe {
6562
// SAFETY: pointer lifetime is greater than the current stack frame
6663
&*e
@@ -107,17 +104,19 @@ pub mod c_api {
107104
struct ParseError {
108105
expected: char,
109106
line: u32,
110-
ch: u16
107+
ch: u16,
111108
}
112109
113-
impl ParseError { /* ... */ }
110+
impl ParseError {
111+
/* ... */
112+
}
114113
115114
/* Create a second version which is exposed as a C structure */
116115
#[repr(C)]
117116
pub struct parse_error {
118117
pub expected: libc::c_char,
119118
pub line: u32,
120-
pub ch: u16
119+
pub ch: u16,
121120
}
122121
123122
impl From<ParseError> for parse_error {

src/idioms/ffi/passing-strings.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ pub mod unsafe_module {
3535
fn geterr(buffer: *mut libc::c_char, size: libc::c_int) -> libc::c_int;
3636
}
3737
38-
fn report_error_to_ffi<S: Into<String>>(
39-
err: S
40-
) -> Result<(), std::ffi::NulError>{
38+
fn report_error_to_ffi<S: Into<String>>(err: S) -> Result<(), std::ffi::NulError> {
4139
let c_err = std::ffi::CString::new(err.into())?;
4240
4341
unsafe {

src/idioms/mem-replace.md

+12-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use std::mem;
1515

1616
enum MyEnum {
1717
A { name: String, x: u8 },
18-
B { name: String }
18+
B { name: String },
1919
}
2020

2121
fn a_to_b(e: &mut MyEnum) {
@@ -24,7 +24,9 @@ fn a_to_b(e: &mut MyEnum) {
2424
// (note that empty strings don't allocate).
2525
// Then, construct the new enum variant (which will
2626
// be assigned to `*e`).
27-
*e = MyEnum::B { name: mem::take(name) }
27+
*e = MyEnum::B {
28+
name: mem::take(name),
29+
}
2830
}
2931
}
3032
```
@@ -38,18 +40,22 @@ enum MultiVariateEnum {
3840
A { name: String },
3941
B { name: String },
4042
C,
41-
D
43+
D,
4244
}
4345

4446
fn swizzle(e: &mut MultiVariateEnum) {
4547
use MultiVariateEnum::*;
4648
*e = match e {
4749
// Ownership rules do not allow taking `name` by value, but we cannot
4850
// take the value out of a mutable reference, unless we replace it:
49-
A { name } => B { name: mem::take(name) },
50-
B { name } => A { name: mem::take(name) },
51+
A { name } => B {
52+
name: mem::take(name),
53+
},
54+
B { name } => A {
55+
name: mem::take(name),
56+
},
5157
C => D,
52-
D => C
58+
D => C,
5359
}
5460
}
5561
```

src/idioms/priv-extend.md

+8-6
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,23 @@ mod a {
2525
pub struct S {
2626
pub foo: i32,
2727
}
28-
28+
2929
#[non_exhaustive]
3030
pub enum AdmitMoreVariants {
3131
VariantA,
3232
VariantB,
3333
#[non_exhaustive]
34-
VariantC { a: String }
34+
VariantC {
35+
a: String,
36+
},
3537
}
3638
}
3739

3840
fn print_matched_variants(s: a::S) {
3941
// Because S is `#[non_exhaustive]`, it cannot be named here and
4042
// we must use `..` in the pattern.
41-
let a::S { foo: _, ..} = s;
42-
43+
let a::S { foo: _, .. } = s;
44+
4345
let some_enum = a::AdmitMoreVariants::VariantA;
4446
match some_enum {
4547
a::AdmitMoreVariants::VariantA => println!("it's an A"),
@@ -50,7 +52,7 @@ fn print_matched_variants(s: a::S) {
5052

5153
// The wildcard match is required because more variants may be
5254
// added in the future
53-
_ => println!("it's a new variant")
55+
_ => println!("it's a new variant"),
5456
}
5557
}
5658
```
@@ -78,7 +80,7 @@ pub struct S {
7880
pub a: i32,
7981
// Because `b` is private, you cannot match on `S` without using `..` and `S`
8082
// cannot be directly instantiated or matched against
81-
_b: ()
83+
_b: (),
8284
}
8385
```
8486

src/idioms/return-consumed-arg-on-error.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ pub fn send(value: String) -> Result<(), SendError> {
1212
println!("using {value} in a meaningful way");
1313
// Simulate non-deterministic fallible action.
1414
use std::time::SystemTime;
15-
let period = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap();
15+
let period = SystemTime::now()
16+
.duration_since(SystemTime::UNIX_EPOCH)
17+
.unwrap();
1618
if period.subsec_nanos() % 2 == 1 {
1719
Ok(())
1820
} else {

src/patterns/behavioural/RAII.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ impl<'a, T> Deref for MutexGuard<'a, T> {
6363
fn baz(x: Mutex<Foo>) {
6464
let xx = x.lock();
6565
xx.foo(); // foo is a method on Foo.
66-
// The borrow checker ensures we can't store a reference to the underlying
67-
// Foo which will outlive the guard xx.
66+
// The borrow checker ensures we can't store a reference to the underlying
67+
// Foo which will outlive the guard xx.
6868
6969
// x is unlocked when we exit this function and xx's destructor is executed.
7070
}

0 commit comments

Comments
 (0)