-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for mut ref dependents (#60)
* Add support for mut ref dependents This has feature that has been requested multiple times by users, #36 and #59. * Fix accidental std usage. * Model unlocked -> locked via unlocked by construction This is less code, less run-time work and conceptually harder to miss-use. Win-win. * Remove unnecessary lock code and switch to AtomicBool
- Loading branch information
1 parent
156818a
commit 10f545e
Showing
12 changed files
with
311 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[package] | ||
name = "owner_with_lifetime" | ||
version = "0.1.0" | ||
authors = ["Lukas Bergdoll <[email protected]>"] | ||
edition = "2018" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
self_cell = { path="../../"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# `mut_ref_to_owner_in_builder` Example | ||
|
||
This example shows how to handle dependent types that want to reference the | ||
owner by `&mut`. This works by using the wrapper type `MutBorrow` around the | ||
owner type. This allows us to call `borrow_mut` in the builder function. This | ||
example also shows how to recover the owner value if desired. | ||
|
||
Run this example with `cargo run`, it should output: | ||
|
||
``` | ||
dependent before pop: abc | ||
dependent after pop: ab | ||
recovered owner: ab | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use self_cell::{self_cell, MutBorrow}; | ||
|
||
type MutStringRef<'a> = &'a mut String; | ||
|
||
self_cell!( | ||
struct MutStringCell { | ||
owner: MutBorrow<String>, | ||
|
||
#[covariant] | ||
dependent: MutStringRef, | ||
} | ||
); | ||
|
||
fn main() { | ||
let mut cell = MutStringCell::new(MutBorrow::new("abc".into()), |owner| owner.borrow_mut()); | ||
|
||
cell.with_dependent_mut(|_owner, dependent| { | ||
println!("dependent before pop: {}", dependent); | ||
dependent.pop(); | ||
println!("dependent after pop: {}", dependent); | ||
}); | ||
|
||
let recovered_owner: String = cell.into_owner().into_inner(); | ||
println!("recovered owner: {}", recovered_owner); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
[package] | ||
name = "owner_with_lifetime" | ||
name = "mut_ref_to_owner_in_builder" | ||
version = "0.1.0" | ||
authors = ["Lukas Bergdoll <[email protected]>"] | ||
edition = "2018" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
self_cell = { path="../../"} | ||
self_cell = { path = "../../" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.