Skip to content

Commit

Permalink
SharedSerial: do not hold a reference, just a lifetime dependency
Browse files Browse the repository at this point in the history
The type parameter also serves to distinguish the SharedSerial created
from various sources.
  • Loading branch information
Chris00 committed Dec 30, 2023
1 parent ad189d0 commit da1d397
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/vector.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Vectors
use std::{mem, slice};
use std::{mem, slice, marker::PhantomData};
use sundials_sys::*;
use super::Context;

Expand Down Expand Up @@ -55,8 +55,8 @@ pub unsafe trait Vector: Clone {
/// Rust value. This is used internally to convert Rust values to
/// appropriate input/outputs for Sundials routines.
pub struct SharedSerial<V> {
v: V,
pub(crate) nv: N_Vector,
marker: PhantomData<V>, // Lifetime of the Rust vector
}

impl<V> Drop for SharedSerial<V> {
Expand Down Expand Up @@ -106,7 +106,7 @@ unsafe impl<const N: usize> Vector for [f64; N] {
N.try_into().unwrap(),
self.as_ptr() as *mut _,
ctx) };
SharedSerial { v: self, nv }
SharedSerial { nv, marker: PhantomData }
}

type NVectorMut<'a> = SharedSerial<&'a mut [f64; N]>;
Expand All @@ -119,7 +119,7 @@ unsafe impl<const N: usize> Vector for [f64; N] {
N.try_into().unwrap(),
self.as_mut_ptr(),
ctx) };
SharedSerial { v: self, nv }
SharedSerial { nv, marker: PhantomData }
}

#[inline]
Expand Down Expand Up @@ -188,7 +188,7 @@ where V: AsVector {
self.len().try_into().unwrap(),
self.as_ref().as_ptr() as *mut _,
ctx) };
SharedSerial {v: self, nv }
SharedSerial { nv, marker: PhantomData }
}

type NVectorMut<'a> = SharedSerial<&'a mut V>;
Expand All @@ -202,7 +202,7 @@ where V: AsVector {
self.len().try_into().unwrap(),
self.as_mut().as_ptr() as *mut _,
ctx) };
SharedSerial {v: self, nv }
SharedSerial { nv, marker: PhantomData }
}

#[inline]
Expand Down

0 comments on commit da1d397

Please sign in to comment.