Skip to content

Commit 1fe5457

Browse files
committed
Fix minor style issues
1 parent 55de6fa commit 1fe5457

30 files changed

+169
-255
lines changed

src/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# The Rustonomicon
22

3-
#### The Dark Arts of Unsafe Rust
3+
## The Dark Arts of Unsafe Rust
44

55
> THE KNOWLEDGE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
66
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF UNLEASHING INDESCRIBABLE HORRORS THAT

src/SUMMARY.md

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,64 +3,64 @@
33
[Introduction](README.md)
44

55
* [Meet Safe and Unsafe](meet-safe-and-unsafe.md)
6-
* [How Safe and Unsafe Interact](safe-unsafe-meaning.md)
7-
* [What Unsafe Can Do](what-unsafe-does.md)
8-
* [Working with Unsafe](working-with-unsafe.md)
6+
* [How Safe and Unsafe Interact](safe-unsafe-meaning.md)
7+
* [What Unsafe Can Do](what-unsafe-does.md)
8+
* [Working with Unsafe](working-with-unsafe.md)
99
* [Data Layout](data.md)
10-
* [repr(Rust)](repr-rust.md)
11-
* [Exotically Sized Types](exotic-sizes.md)
12-
* [Other reprs](other-reprs.md)
10+
* [repr(Rust)](repr-rust.md)
11+
* [Exotically Sized Types](exotic-sizes.md)
12+
* [Other reprs](other-reprs.md)
1313
* [Ownership](ownership.md)
14-
* [References](references.md)
15-
* [Aliasing](aliasing.md)
16-
* [Lifetimes](lifetimes.md)
17-
* [Limits of Lifetimes](lifetime-mismatch.md)
18-
* [Lifetime Elision](lifetime-elision.md)
19-
* [Unbounded Lifetimes](unbounded-lifetimes.md)
20-
* [Higher-Rank Trait Bounds](hrtb.md)
21-
* [Subtyping and Variance](subtyping.md)
22-
* [Drop Check](dropck.md)
23-
* [PhantomData](phantom-data.md)
24-
* [Splitting Borrows](borrow-splitting.md)
14+
* [References](references.md)
15+
* [Aliasing](aliasing.md)
16+
* [Lifetimes](lifetimes.md)
17+
* [Limits of Lifetimes](lifetime-mismatch.md)
18+
* [Lifetime Elision](lifetime-elision.md)
19+
* [Unbounded Lifetimes](unbounded-lifetimes.md)
20+
* [Higher-Rank Trait Bounds](hrtb.md)
21+
* [Subtyping and Variance](subtyping.md)
22+
* [Drop Check](dropck.md)
23+
* [PhantomData](phantom-data.md)
24+
* [Splitting Borrows](borrow-splitting.md)
2525
* [Type Conversions](conversions.md)
26-
* [Coercions](coercions.md)
27-
* [The Dot Operator](dot-operator.md)
28-
* [Casts](casts.md)
29-
* [Transmutes](transmutes.md)
26+
* [Coercions](coercions.md)
27+
* [The Dot Operator](dot-operator.md)
28+
* [Casts](casts.md)
29+
* [Transmutes](transmutes.md)
3030
* [Uninitialized Memory](uninitialized.md)
31-
* [Checked](checked-uninit.md)
32-
* [Drop Flags](drop-flags.md)
33-
* [Unchecked](unchecked-uninit.md)
31+
* [Checked](checked-uninit.md)
32+
* [Drop Flags](drop-flags.md)
33+
* [Unchecked](unchecked-uninit.md)
3434
* [Ownership Based Resource Management](obrm.md)
35-
* [Constructors](constructors.md)
36-
* [Destructors](destructors.md)
37-
* [Leaking](leaking.md)
35+
* [Constructors](constructors.md)
36+
* [Destructors](destructors.md)
37+
* [Leaking](leaking.md)
3838
* [Unwinding](unwinding.md)
39-
* [Exception Safety](exception-safety.md)
40-
* [Poisoning](poisoning.md)
39+
* [Exception Safety](exception-safety.md)
40+
* [Poisoning](poisoning.md)
4141
* [Concurrency](concurrency.md)
42-
* [Races](races.md)
43-
* [Send and Sync](send-and-sync.md)
44-
* [Atomics](atomics.md)
42+
* [Races](races.md)
43+
* [Send and Sync](send-and-sync.md)
44+
* [Atomics](atomics.md)
4545
* [Implementing Vec](vec.md)
46-
* [Layout](vec-layout.md)
47-
* [Allocating](vec-alloc.md)
48-
* [Push and Pop](vec-push-pop.md)
49-
* [Deallocating](vec-dealloc.md)
50-
* [Deref](vec-deref.md)
51-
* [Insert and Remove](vec-insert-remove.md)
52-
* [IntoIter](vec-into-iter.md)
53-
* [RawVec](vec-raw.md)
54-
* [Drain](vec-drain.md)
55-
* [Handling Zero-Sized Types](vec-zsts.md)
56-
* [Final Code](vec-final.md)
46+
* [Layout](vec-layout.md)
47+
* [Allocating](vec-alloc.md)
48+
* [Push and Pop](vec-push-pop.md)
49+
* [Deallocating](vec-dealloc.md)
50+
* [Deref](vec-deref.md)
51+
* [Insert and Remove](vec-insert-remove.md)
52+
* [IntoIter](vec-into-iter.md)
53+
* [RawVec](vec-raw.md)
54+
* [Drain](vec-drain.md)
55+
* [Handling Zero-Sized Types](vec-zsts.md)
56+
* [Final Code](vec-final.md)
5757
* [Implementing Arc and Mutex](arc-and-mutex.md)
58-
* [Arc](arc.md)
59-
* [Layout](arc-layout.md)
60-
* [Base Code](arc-base.md)
61-
* [Cloning](arc-clone.md)
62-
* [Dropping](arc-drop.md)
63-
* [Final Code](arc-final.md)
58+
* [Arc](arc.md)
59+
* [Layout](arc-layout.md)
60+
* [Base Code](arc-base.md)
61+
* [Cloning](arc-clone.md)
62+
* [Dropping](arc-drop.md)
63+
* [Final Code](arc-final.md)
6464
* [FFI](ffi.md)
6565
* [Beneath `std`](beneath-std.md)
66-
* [#[panic_handler]](panic-handler.md)
66+
* [#[panic_handler]](panic-handler.md)

src/aliasing.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ don't happen unless you tell it otherwise. For more details, see the
1414
With that said, here's our working definition: variables and pointers *alias*
1515
if they refer to overlapping regions of memory.
1616

17-
18-
19-
20-
# Why Aliasing Matters
17+
## Why Aliasing Matters
2118

2219
So why should we care about aliasing?
2320

@@ -130,6 +127,3 @@ Of course, a full aliasing model for Rust must also take into consideration thin
130127
function calls (which may mutate things we don't see), raw pointers (which have
131128
no aliasing requirements on their own), and UnsafeCell (which lets the referent
132129
of an `&` be mutated).
133-
134-
135-

src/arc-clone.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@
33
Now that we've got some basic code set up, we'll need a way to clone the `Arc`.
44

55
Basically, we need to:
6+
67
1. Increment the atomic reference count
78
2. Construct a new instance of the `Arc` from the inner pointer
89

910
First, we need to get access to the `ArcInner`:
11+
1012
```rust,ignore
1113
let inner = unsafe { self.ptr.as_ref() };
1214
```
1315

1416
We can update the atomic reference count as follows:
17+
1518
```rust,ignore
1619
let old_rc = inner.rc.fetch_add(1, Ordering::???);
1720
```
@@ -26,11 +29,13 @@ is described more in [the section on the `Drop` implementation for
2629
ordering, see [the section on atomics](atomics.md).
2730

2831
Thus, the code becomes this:
32+
2933
```rust,ignore
3034
let old_rc = inner.rc.fetch_add(1, Ordering::Relaxed);
3135
```
3236

3337
We'll need to add another import to use `Ordering`:
38+
3439
```rust,ignore
3540
use std::sync::atomic::Ordering;
3641
```
@@ -54,14 +59,16 @@ probably incredibly degenerate) if the reference count reaches `isize::MAX`
5459
probably not about 2 billion threads (or about **9 quintillion** on some 64-bit
5560
machines) incrementing the reference count at once. This is what we'll do.
5661

57-
It's pretty simple to implement this behaviour:
62+
It's pretty simple to implement this behavior:
63+
5864
```rust,ignore
5965
if old_rc >= isize::MAX as usize {
6066
std::process::abort();
6167
}
6268
```
6369

6470
Then, we need to return a new instance of the `Arc`:
71+
6572
```rust,ignore
6673
Self {
6774
ptr: self.ptr,
@@ -70,6 +77,7 @@ Self {
7077
```
7178

7279
Now, let's wrap this all up inside the `Clone` implementation:
80+
7381
```rust,ignore
7482
use std::sync::atomic::Ordering;
7583

src/arc-drop.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ low enough, otherwise the data will live forever on the heap.
66
To do this, we can implement `Drop`.
77

88
Basically, we need to:
9+
910
1. Decrement the reference count
1011
2. If there is only one reference remaining to the data, then:
1112
3. Atomically fence the data to prevent reordering of the use and deletion of
1213
the data
13-
4. Drop the inner data
14+
4. Drop the inner data
1415

1516
First, we'll need to get access to the `ArcInner`:
17+
1618
```rust,ignore
1719
let inner = unsafe { self.ptr.as_ref() };
1820
```
@@ -21,6 +23,7 @@ Now, we need to decrement the reference count. To streamline our code, we can
2123
also return if the returned value from `fetch_sub` (the value of the reference
2224
count before decrementing it) is not equal to `1` (which happens when we are not
2325
the last reference to the data).
26+
2427
```rust,ignore
2528
if inner.rc.fetch_sub(1, Ordering::Relaxed) != 1 {
2629
return;
@@ -53,17 +56,19 @@ implementation of `Arc`][3]:
5356
> Also note that the Acquire fence here could probably be replaced with an
5457
> Acquire load, which could improve performance in highly-contended situations.
5558
> See [2].
56-
>
59+
>
5760
> [1]: https://www.boost.org/doc/libs/1_55_0/doc/html/atomic/usage_examples.html
5861
> [2]: https://github.com/rust-lang/rust/pull/41714
5962
[3]: https://github.com/rust-lang/rust/blob/e1884a8e3c3e813aada8254edfa120e85bf5ffca/library/alloc/src/sync.rs#L1440-L1467
6063

6164
To do this, we do the following:
65+
6266
```rust,ignore
6367
atomic::fence(Ordering::Acquire);
6468
```
6569

6670
We'll need to import `std::sync::atomic` itself:
71+
6772
```rust,ignore
6873
use std::sync::atomic;
6974
```
@@ -80,6 +85,7 @@ This is safe as we know we have the last pointer to the `ArcInner` and that its
8085
pointer is valid.
8186

8287
Now, let's wrap this all up inside the `Drop` implementation:
88+
8389
```rust,ignore
8490
impl<T> Drop for Arc<T> {
8591
fn drop(&mut self) {

src/arc-final.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Final Code
22

33
Here's the final code, with some added comments and re-ordered imports:
4+
45
```rust
56
use std::marker::PhantomData;
67
use std::ops::Deref;

src/arc-layout.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pointer to the T's allocation, we might as well put the reference count in that
2121
same allocation.
2222

2323
Naively, it would look something like this:
24+
2425
```rust,ignore
2526
use std::sync::atomic;
2627
@@ -45,6 +46,7 @@ all the details on variance and drop check.
4546

4647
To fix the first problem, we can use `NonNull<T>`. Note that `NonNull<T>` is a
4748
wrapper around a raw pointer that declares that:
49+
4850
* We are variant over `T`
4951
* Our pointer is never null
5052

@@ -53,6 +55,7 @@ To fix the second problem, we can include a `PhantomData` marker containing an
5355
ownership of a value of `ArcInner<T>` (which itself contains some `T`).
5456

5557
With these changes we get our final structure:
58+
5659
```rust,ignore
5760
use std::marker::PhantomData;
5861
use std::ptr::NonNull;

src/atomics.md

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ semantics we want, the optimizations compilers want, and the inconsistent chaos
2222
our hardware wants. *We* would like to just write programs and have them do
2323
exactly what we said but, you know, fast. Wouldn't that be great?
2424

25-
26-
27-
28-
# Compiler Reordering
25+
## Compiler Reordering
2926

3027
Compilers fundamentally want to be able to do all sorts of complicated
3128
transformations to reduce data dependencies and eliminate dead code. In
@@ -54,10 +51,7 @@ able to make these kinds of optimizations, because they can seriously improve
5451
performance. On the other hand, we'd also like to be able to depend on our
5552
program *doing the thing we said*.
5653

57-
58-
59-
60-
# Hardware Reordering
54+
## Hardware Reordering
6155

6256
On the other hand, even if the compiler totally understood what we wanted and
6357
respected our wishes, our hardware might instead get us in trouble. Trouble
@@ -110,11 +104,7 @@ programming:
110104
incorrect. If possible, concurrent algorithms should be tested on
111105
weakly-ordered hardware.
112106

113-
114-
115-
116-
117-
# Data Accesses
107+
## Data Accesses
118108

119109
The C++ memory model attempts to bridge the gap by allowing us to talk about the
120110
*causality* of our program. Generally, this is by establishing a *happens
@@ -156,9 +146,7 @@ propagated to other threads. The set of orderings Rust exposes are:
156146
TODO: negative reasoning vs positive reasoning? TODO: "can't forget to
157147
synchronize"
158148

159-
160-
161-
# Sequentially Consistent
149+
## Sequentially Consistent
162150

163151
Sequentially Consistent is the most powerful of all, implying the restrictions
164152
of all other orderings. Intuitively, a sequentially consistent operation
@@ -182,10 +170,7 @@ mechanically trivial to downgrade atomic operations to have a weaker
182170
consistency later on. Just change `SeqCst` to `Relaxed` and you're done! Of
183171
course, proving that this transformation is *correct* is a whole other matter.
184172

185-
186-
187-
188-
# Acquire-Release
173+
## Acquire-Release
189174

190175
Acquire and Release are largely intended to be paired. Their names hint at their
191176
use case: they're perfectly suited for acquiring and releasing locks, and
@@ -200,8 +185,8 @@ reordered to occur before it.
200185
When thread A releases a location in memory and then thread B subsequently
201186
acquires *the same* location in memory, causality is established. Every write
202187
(including non-atomic and relaxed atomic writes) that happened before A's
203-
release will be observed by B after its acquisition. However no causality is
204-
established with any other threads. Similarly, no causality is established
188+
release will be observed by B after its acquisition. However no causality is
189+
established with any other threads. Similarly, no causality is established
205190
if A and B access *different* locations in memory.
206191

207192
Basic use of release-acquire is therefore simple: you acquire a location of
@@ -233,10 +218,7 @@ On strongly-ordered platforms most accesses have release or acquire semantics,
233218
making release and acquire often totally free. This is not the case on
234219
weakly-ordered platforms.
235220

236-
237-
238-
239-
# Relaxed
221+
## Relaxed
240222

241223
Relaxed accesses are the absolute weakest. They can be freely re-ordered and
242224
provide no happens-before relationship. Still, relaxed operations are still
@@ -251,9 +233,5 @@ There's rarely a benefit in making an operation relaxed on strongly-ordered
251233
platforms, since they usually provide release-acquire semantics anyway. However
252234
relaxed operations can be cheaper on weakly-ordered platforms.
253235

254-
255-
256-
257-
258236
[C11-busted]: http://plv.mpi-sws.org/c11comp/popl15.pdf
259237
[C++-model]: https://en.cppreference.com/w/cpp/atomic/memory_order

src/beneath-std.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This section documents (or will document) features that are provided by the stan
44
that `#![no_std]` developers have to deal with (i.e. provide) to build `#![no_std]` binary crates. A
55
(likely incomplete) list of such features is shown below:
66

7-
- #[lang = "eh_personality"]
8-
- #[lang = "start"]
9-
- #[lang = "termination"]
10-
- #[panic_implementation]
7+
- `#[lang = "eh_personality"]`
8+
- `#[lang = "start"]`
9+
- `#[lang = "termination"]`
10+
- `#[panic_implementation]`

0 commit comments

Comments
 (0)