Skip to content

Commit a6cf6a6

Browse files
authored
Merge pull request #17 from Nadrieril/uniquearc
2 parents 21f89fb + f13ce19 commit a6cf6a6

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

src/examples/unique-arc.md

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,36 @@ impl<T> UniqueArc<T> {
1919
}
2020
```
2121

22-
It would be nice to be able to borrow its fields somehow. I do not know how to manage the refcounts
23-
for that to work, plz fix if anyone knows.
22+
[ACP 700](https://github.com/rust-lang/libs-team/issues/700) proposes a way to field-project
23+
a `UniqueArc`: first we change how strong counts work. Instead of just a strong count, we cram two
24+
counters into the `u64`, one for shared refs and one for unique refs, and update the rest of the
25+
logic accordingly. Can't upgrade a weak pointer if there are any unique refs.
2426
```rust
27+
impl<T> UniqueArc<T> {
28+
// Checks the unique-ptr count.
29+
pub fn try_into_arc(self) -> Result<Arc<T>, NotUnique> { .. }
30+
}
31+
2532
/// A subplace of a `UniqueArc`.
26-
// I have no idea how to manage the reference counts in a way that works.
2733
pub struct UniqueArcMap<T> {
2834
/// Pointer to the reference counts.
2935
header: NonNull<ArcHeader>,
3036
/// Pointer to the subplace we care about.
3137
val: NonNull<T>,
3238
}
3339

34-
// Intended permissions: read, write, borrow fields as `&`, `&mut`, reborrow fields as `UniqueArcMap`
35-
// The original `UniqueArc::into_arc` should only be allowed after all the `UniqueArcMap`s have
36-
// expired.
40+
// Supports: read, write, borrow fields as `&`, `&mut`, reborrow fields as `UniqueArcMap`
41+
```
42+
43+
The issue then is:
44+
- If we don't give `UniqueArcMap` special borrowck behavior, then disjoint borrowing must be done
45+
with methods that split the borrow; that's sad.
46+
- If we do give `UniqueArcMap` special borrowck behavior, then we can prevent multiple
47+
`@UniqueArcMap x.field` to the same fields. However, we can't get back a `Arc<T>` anymore:
48+
```rust
49+
let x: UniqueArc<Foo> = ...;
50+
let field = @UniqueArcMap x.field;
51+
// can't access `x.field` anymore
52+
// in particular, can't call a method on `x` itself, since that may access `x.field`:
53+
let arc = x.try_into_arc()?; // ERROR `x.field` is borrowed
3754
```

0 commit comments

Comments
 (0)