-
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.
- Loading branch information
1 parent
156818a
commit cb88ca8
Showing
10 changed files
with
453 additions
and
8 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
Oops, something went wrong.