Merge pull request #1395 from nyurik/mut-window-value

Make WindowAggregate::value pass mutable value ref
This commit is contained in:
gwenn 2023-10-22 08:57:36 +02:00 committed by GitHub
commit 46430e0362
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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<T>;
fn value(&self, acc: Option<&mut A>) -> Result<T>;
/// 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) =>
{
let pac = aggregate_context(ctx, 0).filter(|&pac| {
#[allow(clippy::unnecessary_cast)]
if (*pac as *mut A).is_null() {
None
} else {
let a = &**pac;
Some(a)
}
}
None => None,
};
!(*pac as *mut A).is_null()
});
let r = catch_unwind(|| {
let boxed_aggr: *mut W = ffi::sqlite3_user_data(ctx).cast::<W>();
@ -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<Option<i64>> {
fn value(&self, sum: Option<&mut i64>) -> Result<Option<i64>> {
Ok(sum.copied())
}
}