Skip to content

Commit 05bb1db

Browse files
committed
OBRM for aturon
1 parent fd13bdf commit 05bb1db

File tree

5 files changed

+50
-26
lines changed

5 files changed

+50
-26
lines changed

src/doc/tarpl/SUMMARY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* [Checked](checked-uninit.md)
2828
* [Drop Flags](drop-flags.md)
2929
* [Unchecked](unchecked-uninit.md)
30-
* [Ownership-Oriented Resource Management](raii.md)
30+
* [Ownership Based Resource Management](obrm.md)
3131
* [Constructors](constructors.md)
3232
* [Destructors](destructors.md)
3333
* [Leaking](leaking.md)

src/doc/tarpl/destructors.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ impl<T> Drop for Box<T> {
4545
```
4646

4747
and this works fine because when Rust goes to drop the `ptr` field it just sees
48-
a *mut that has no actual `Drop` implementation. Similarly nothing can use-
49-
after-free the `ptr` because the Box is immediately marked as uninitialized.
48+
a [Unique][] that has no actual `Drop` implementation. Similarly nothing can
49+
use-after-free the `ptr` because when drop exits, it becomes inacessible.
5050

5151
However this wouldn't work:
5252

@@ -174,3 +174,5 @@ arbitrarily invalid state in there.
174174
On balance this is an ok choice. Certainly what you should reach for by default.
175175
However, in the future we expect there to be a first-class way to announce that
176176
a field shouldn't be automatically dropped.
177+
178+
[Unique]: phantom-data.html

src/doc/tarpl/leaking.md

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,17 @@ is perfect and all of our problems are solved.
88

99
Everything is terrible and we have new and exotic problems to try to solve.
1010

11-
Many people like to believe that Rust eliminates resource leaks, but this is
12-
absolutely not the case, no matter how you look at it. In the strictest sense,
13-
"leaking" is so abstract as to be unpreventable. It's quite trivial to
14-
initialize a collection at the start of a program, fill it with tons of objects
15-
with destructors, and then enter an infinite event loop that never refers to it.
16-
The collection will sit around uselessly, holding on to its precious resources
17-
until the program terminates (at which point all those resources would have been
18-
reclaimed by the OS anyway).
11+
Many people like to believe that Rust eliminates resource leaks. In practice,
12+
this is basically true. You would be surprised to see a Safe Rust program
13+
leak resources in an uncontrolled way.
14+
15+
However from a theoretical perspective this is absolutely not the case, no
16+
matter how you look at it. In the strictest sense, "leaking" is so abstract as
17+
to be unpreventable. It's quite trivial to initialize a collection at the start
18+
of a program, fill it with tons of objects with destructors, and then enter an
19+
infinite event loop that never refers to it. The collection will sit around
20+
uselessly, holding on to its precious resources until the program terminates (at
21+
which point all those resources would have been reclaimed by the OS anyway).
1922

2023
We may consider a more restricted form of leak: failing to drop a value that is
2124
unreachable. Rust also doesn't prevent this. In fact Rust has a *function for
@@ -181,7 +184,26 @@ in memory.
181184
## thread::scoped::JoinGuard
182185

183186
The thread::scoped API intends to allow threads to be spawned that reference
184-
data on the stack without any synchronization over that data. Usage looked like:
187+
data on their parent's stack without any synchronization over that data by
188+
ensuring the parent joins the thread before any of the shared data goes out
189+
of scope.
190+
191+
```rust
192+
pub fn scoped<'a, F>(f: F) -> JoinGuard<'a>
193+
where F: FnOnce() + Send + 'a
194+
```
195+
196+
Here `f` is some closure for the other thread to execute. Saying that
197+
`F: Send +'a` is saying that it closes over data that lives for `'a`, and it
198+
either owns that data or the data was Sync (implying `&data` is Send).
199+
200+
Because JoinGuard has a lifetime, it keeps all the data it closes over
201+
borrowed in the parent thread. This means the JoinGuard can't outlive
202+
the data that the other thread is working on. When the JoinGuard *does* get
203+
dropped it blocks the parent thread, ensuring the child terminates before any
204+
of the closed-over data goes out of scope in the parent.
205+
206+
Usage looked like:
185207

186208
```rust,ignore
187209
let mut data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

src/doc/tarpl/obrm.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
% The Perils Of Ownership Based Resource Management (OBRM)
2+
3+
OBRM (AKA RAII: Resource Acquisition Is Initialization) is something you'll
4+
interact with a lot in Rust. Especially if you use the standard library.
5+
6+
Roughly speaking the pattern is as follows: to acquire a resource, you create an
7+
object that manages it. To release the resource, you simply destroy the object,
8+
and it cleans up the resource for you. The most common "resource" this pattern
9+
manages is simply *memory*. `Box`, `Rc`, and basically everything in
10+
`std::collections` is a convenience to enable correctly managing memory. This is
11+
particularly important in Rust because we have no pervasive GC to rely on for
12+
memory management. Which is the point, really: Rust is about control. However we
13+
are not limited to just memory. Pretty much every other system resource like a
14+
thread, file, or socket is exposed through this kind of API.

src/doc/tarpl/raii.md

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)