From: Ian Jackson Date: Fri, 29 Apr 2022 23:05:35 +0000 (+0100) Subject: Introduce and use PrepareUpdatesBuffer::only_unprepared_with X-Git-Tag: otter-1.1.0~423 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=621ed237f1a0eed7031161dce9b026d2683cd166;p=otter.git Introduce and use PrepareUpdatesBuffer::only_unprepared_with This saves us writing `if unprepared.len() != 0`. I think that pattern is asking for a reversed conditional at one random site. Signed-off-by: Ian Jackson --- diff --git a/daemon/cmdlistener.rs b/daemon/cmdlistener.rs index 2ee41962..3bd816be 100644 --- a/daemon/cmdlistener.rs +++ b/daemon/cmdlistener.rs @@ -1337,12 +1337,10 @@ fn execute_for_game<'cs, 'igr, 'ig: 'igr>( assert!(expand.is_empty()) } responses.push(resp); - if let Some(unprepared) = unprepared { + PrepareUpdatesBuffer::only_unprepared_with(unprepared, ||{ st.flush(ig,how,&who)?; - let mut prepub = PrepareUpdatesBuffer::new(ig, None); - unprepared(&mut prepub); - prepub.finish(); - } + Ok::<_,ME>(ig) + })?; } if let Some(ref mut st) = uh_auth { flush_uh(st,igu)?; @@ -1370,12 +1368,9 @@ fn execute_for_game<'cs, 'igr, 'ig: 'igr>( }) }); - if let Some(uu) = uu { - let ig = igu.by_mut(Authorisation::promise_any()); - let mut prepub = PrepareUpdatesBuffer::new(ig, None); - uu(&mut prepub); - prepub.finish(); - } + PrepareUpdatesBuffer::only_unprepared_with(uu, ||Ok::<_,Void>( + igu.by_mut(Authorisation::promise_any()) + )).void_unwrap(); ok? } diff --git a/src/updates.rs b/src/updates.rs index ba22714f..ec2ab6bf 100644 --- a/src/updates.rs +++ b/src/updates.rs @@ -826,9 +826,18 @@ impl<'r> PrepareUpdatesBuffer<'r> { } pub fn only_unprepared(ig: &'r mut Instance, unprepared: UnpreparedUpdates) { - if let Some(unprepared) = unprepared { + Self::only_unprepared_with(unprepared, ||Ok::<_,Void>(ig)) + .void_unwrap(); + } + + #[throws(E)] + pub fn only_unprepared_with<'i,F,E>(unprepared: UnpreparedUpdates, igf: F) + where F: FnOnce() -> Result<&'i mut Instance, E> + { + if unprepared.is_some() { + let ig = igf()?; let mut prepub = PrepareUpdatesBuffer::new(ig, None); - unprepared(&mut prepub); + prepub.add_unprepared(unprepared); prepub.finish(); } }