Lynnesbian :bune_ylw: on Nostr: step 1: i want to keep a mutable reference to an element of an RwLock so i don't have ...
step 1: i want to keep a mutable reference to an element of an RwLock so i don't have to keep writing it out every time
step 2: ah, i can't do that because the &mut would outlive the mutex guard. i'll create a block to scope the reference inside the mutex's lifetime
step 3: this works, but i need to call self.foo() which takes &mut self. this would mean having two mutable references to self simultanously, which isn't allowed
step 4: i'll create a closure to reduce the scope of the mutex ref. you'll have to use with_thingy(|thingy| thingy.whatever()), but it works
step 5: i can't do that because closures can't be cast to fn() pointers if they capture their environments, and i can't use impl Fn() because you can't use impl Trait in closure arguments.
now i've ended up with this abomination: a closure that takes a boxed closure that takes the mutable reference. i think i should have abandoned this approach somewhere around step 4
#rust
Published at
2026-04-29 23:52:30 UTCEvent JSON
{
"id": "1f6f1cdc15d803ca8e42580d82f32c38f9f64cdf6a30e1951fbe02806b4a4914",
"pubkey": "0c73d31439e4af05d0eb30069e1e8ee3f5993b569c9932007c59d2ba0cc25d99",
"created_at": 1777506750,
"kind": 1,
"tags": [
[
"proxy",
"https://fedi.lynnesbian.space/@lynnesbian/116490682411621136",
"web"
],
[
"content-warning",
"programming, technical, bad ideas, rust"
],
[
"t",
"rust"
],
[
"imeta",
"url https://wasabi.lynnesbian.space/the-slum-bucket/media_attachments/files/116/490/674/337/051/737/original/9e02cfdc06f0fe9d.png",
"m image/png"
],
[
"proxy",
"https://fedi.lynnesbian.space/users/lynnesbian/statuses/116490682411621136",
"activitypub"
],
[
"L",
"pink.momostr"
],
[
"l",
"pink.momostr.activitypub:https://fedi.lynnesbian.space/users/lynnesbian/statuses/116490682411621136",
"pink.momostr"
],
[
"-"
]
],
"content": "step 1: i want to keep a mutable reference to an element of an RwLock so i don't have to keep writing it out every time\nstep 2: ah, i can't do that because the \u0026amp;mut would outlive the mutex guard. i'll create a block to scope the reference inside the mutex's lifetime\nstep 3: this works, but i need to call self.foo() which takes \u0026amp;mut self. this would mean having two mutable references to self simultanously, which isn't allowed\nstep 4: i'll create a closure to reduce the scope of the mutex ref. you'll have to use with_thingy(|thingy| thingy.whatever()), but it works\nstep 5: i can't do that because closures can't be cast to fn() pointers if they capture their environments, and i can't use impl Fn() because you can't use impl Trait in closure arguments.\n\nnow i've ended up with this abomination: a closure that takes a boxed closure that takes the mutable reference. i think i should have abandoned this approach somewhere around step 4\n\n#rust\nhttps://wasabi.lynnesbian.space/the-slum-bucket/media_attachments/files/116/490/674/337/051/737/original/9e02cfdc06f0fe9d.png\n",
"sig": "376434401df7d69c48bf7f3c4a5ee47d79dbb0701587855349d82c00975429b56296873cdafef9967d68320ca249d5cb7bbceef35581ebac584f24f87c5774d1"
}