issues with Lifetime branding example
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 68/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Active
- Tech stack
- rust
- Domain
- documentation
Research direction
Start in rust-patterns-book/src/ch04-phantomdata-types-that-carry-no-data.md around line 44 and review the Lifetime branding example and its surrounding explanation. Compile both arrangements shown in the issue, then update the example so handles cannot be used with a different arena and verify the intended success and failure cases.
Written by the indexing model from the issue text.
Description
The example as written fails to compile when we uncomment the code:
fn main() {
with_arena(|arena1| {
let handle1 = arena1.alloc("hello".to_string());
println!("{}", arena1.get(&handle1)); // ✅
// Can't use handle1 with a different arena — compile-time error
with_arena(|arena2| {
arena2.get(&handle1); // ❌ borrowed data escapes outside of closure
});
});
}
If we rearrange the code a bit:
fn main() {
with_arena(|arena1| {
// Can't use handle1 with a different arena — compile-time error
with_arena(|arena2| {
let handle1 = arena1.alloc("hello".to_string());
println!("{}", arena1.get(&handle1)); // ✅
arena2.get(&handle1); // ❌ borrowed data escapes outside of closure
});
});
}
This compiles - even though it isn't supposed to.
By moving the brand to the Arena instead of keeping it on the ArenaHandle it works as intended:
/// A handle branded to a specific arena instance.
/// Invariant over 'arena — prevents using a handle from one arena with another.
struct ArenaHandle<'arena> {
index: usize,
_phantom: PhantomData<&'arena ()>,
}
/// An arena that brands each handle with its unique lifetime.
struct Arena<'arena> {
data: RefCell<Vec<String>>,
_brand: PhantomData<*mut &'arena ()>,
}
- Dominant language
- Rust
- Stars
- 14.9k
- Forks
- 1.2k
- Avg merge
- 13d 16h
- Merged PRs (30d)
- 3
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
Eynzof/Hermes-CN-Desktop#616 ·
-
bug rules
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
app bug
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
IronCoreLabs/ironcore-alloy#346 ·
-
good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 65/100