From 3b70307a9462b25ba7cb27b27d234587822ed287 Mon Sep 17 00:00:00 2001 From: Yuri Astrakhan Date: Fri, 6 Oct 2023 17:21:19 -0400 Subject: [PATCH] Make WindowAggregate::value pass mutable value ref --- src/functions.rs | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/src/functions.rs b/src/functions.rs index 8f34f31..522f116 100644 --- a/src/functions.rs +++ b/src/functions.rs @@ -301,7 +301,7 @@ where { /// Returns the current value of the aggregate. Unlike xFinal, the /// implementation should not delete any context. - fn value(&self, acc: Option<&A>) -> Result; + fn value(&self, acc: Option<&mut A>) -> Result; /// Removes a row from the current window. fn inverse(&self, ctx: &mut Context<'_>, acc: &mut A) -> Result<()>; @@ -755,19 +755,10 @@ where { // Within the xValue callback, it is customary to set N=0 in calls to // sqlite3_aggregate_context(C,N) so that no pointless memory allocations occur. - let a: Option<&A> = match aggregate_context(ctx, 0) { - Some(pac) => - { - #[allow(clippy::unnecessary_cast)] - if (*pac as *mut A).is_null() { - None - } else { - let a = &**pac; - Some(a) - } - } - None => None, - }; + let pac = aggregate_context(ctx, 0).filter(|&pac| { + #[allow(clippy::unnecessary_cast)] + !(*pac as *mut A).is_null() + }); let r = catch_unwind(|| { let boxed_aggr: *mut W = ffi::sqlite3_user_data(ctx).cast::(); @@ -775,7 +766,7 @@ where !boxed_aggr.is_null(), "Internal error - null aggregate pointer" ); - (*boxed_aggr).value(a) + (*boxed_aggr).value(pac.map(|pac| &mut **pac)) }); let t = match r { Err(_) => { @@ -1030,7 +1021,7 @@ mod test { Ok(()) } - fn value(&self, sum: Option<&i64>) -> Result> { + fn value(&self, sum: Option<&mut i64>) -> Result> { Ok(sum.copied()) } }