Skip to content

Commit

Permalink
Fix borrowck for pg12
Browse files Browse the repository at this point in the history
  • Loading branch information
workingjubilee committed Oct 31, 2023
1 parent 150fa16 commit d1ee368
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions pgrx/src/list/linked_list.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::{seal, Enlist, List, ListCell, ListHead};
use crate::mem::MemCx;
use crate::pg_sys;
use core::cmp;
use core::ffi;
Expand Down Expand Up @@ -96,7 +97,7 @@ unsafe impl Enlist for pg_sys::Oid {
}
}

impl<T: Enlist> List<T> {
impl<'cx, T: Enlist> List<'cx, T> {
/// Borrow an item from the slice at the index
pub fn get(&self, index: usize) -> Option<&T> {
self.iter().nth(index)
Expand All @@ -113,13 +114,17 @@ impl<T: Enlist> List<T> {
/// otherwise uses the List's own context.
///
/// "Unstable" because this may receive breaking changes.
pub fn unstable_push_in_context(&mut self, value: T, mcx: &MemCx<'_>) -> &mut ListHead<T> {
pub fn unstable_push_in_context(
&mut self,
value: T,
mcx: &'cx MemCx<'_>,
) -> &mut ListHead<'cx, T> {
match self {
List::Nil => unsafe {
let list: *mut pg_sys::List =
mcx.alloc_bytes(context, mem::size_of::<pg_sys::List>()).cast();
mcx.alloc_bytes(mem::size_of::<pg_sys::List>()).cast();
let node: *mut pg_sys::ListCell =
mcx.alloc_bytes(context, mem::size_of::<pg_sys::ListCell>()).cast();
mcx.alloc_bytes(mem::size_of::<pg_sys::ListCell>()).cast();
(*node).next = ptr::null_mut();
*T::apoptosis(node) = value;
(*list).head = node;
Expand All @@ -139,7 +144,7 @@ impl<T: Enlist> List<T> {
/// Attempt to push or Err if it would allocate
///
/// This exists primarily to allow working with a list with maybe-zero capacity.
pub fn try_push(&mut self, value: T) -> Result<&mut ListHead<T>, &mut Self> {
pub fn try_push(&mut self, value: T) -> Result<&mut ListHead<'cx, T>, &mut Self> {
match self {
List::Nil => Err(self),
list if list.capacity() - list.len() == 0 => Err(list),
Expand All @@ -148,7 +153,7 @@ impl<T: Enlist> List<T> {
}

/// Try to reserve space for N more items
pub fn try_reserve(&mut self, items: usize) -> Result<&mut ListHead<T>, &mut Self> {
pub fn try_reserve(&mut self, items: usize) -> Result<&mut ListHead<'cx, T>, &mut Self> {
match self {
List::Nil => Err(self),
List::Cons(head) => Ok(head.reserve(items)),
Expand All @@ -159,7 +164,7 @@ impl<T: Enlist> List<T> {
//
// Note that if this removes the last item, it deallocates the entire list.
// This is to maintain the Postgres List invariant that a 0-len list is always Nil.
pub fn drain<R>(&mut self, range: R) -> Drain<'_, T>
pub fn drain<R>(&mut self, range: R) -> Drain<'_, 'cx, T>
where
R: RangeBounds<usize>,
{
Expand Down Expand Up @@ -266,7 +271,7 @@ impl<T: Enlist> List<T> {
}
}

impl<T> ListHead<T> {
impl<T> ListHead<'_, T> {
/// Nonsensical question in Postgres 11-12, but answers as if len
#[inline]
pub fn capacity(&self) -> usize {
Expand All @@ -282,7 +287,7 @@ impl<T> ListHead<T> {
}
}

impl<T: Enlist> ListHead<T> {
impl<T: Enlist> ListHead<'_, T> {
pub fn push(&mut self, value: T) -> &mut Self {
unsafe {
let list = self.list.as_mut();
Expand Down Expand Up @@ -365,25 +370,25 @@ impl<'a, T: Enlist> Iterator for IterMut<'a, T> {
}

#[derive(Debug)]
pub struct ListIter<T> {
list: List<T>,
pub struct ListIter<'a, T> {
list: List<'a, T>,
iter: RawCellIter<T>,
}

/// A list being drained.
#[derive(Debug)]
pub struct Drain<'a, T> {
pub struct Drain<'a, 'cx, T> {
/// pointer to the cell to append tail to
drain_prefix: *mut ListCell<T>,
/// Length of tail
tail_len: u32,
iter: RawCellIter<T>,
left: u32,
origin: &'a mut List<T>,
origin: &'a mut List<'cx, T>,
raw: *mut pg_sys::List,
}

impl<T> Drop for Drain<'_, T> {
impl<T> Drop for Drain<'_, '_, T> {
fn drop(&mut self) {
if self.raw == ptr::null_mut() {
return;
Expand Down Expand Up @@ -426,7 +431,7 @@ impl<T> Drop for Drain<'_, T> {
}
}

impl<T: Enlist> Iterator for Drain<'_, T> {
impl<T: Enlist> Iterator for Drain<'_, '_, T> {
type Item = T;

fn next(&mut self) -> Option<Self::Item> {
Expand All @@ -437,16 +442,16 @@ impl<T: Enlist> Iterator for Drain<'_, T> {
}
}

impl<T: Enlist> Iterator for ListIter<T> {
impl<T: Enlist> Iterator for ListIter<'_, T> {
type Item = T;

fn next(&mut self) -> Option<Self::Item> {
self.iter.next()
}
}

impl<T: Enlist> IntoIterator for List<T> {
type IntoIter = ListIter<T>;
impl<'cx, T: Enlist> IntoIterator for List<'cx, T> {
type IntoIter = ListIter<'cx, T>;
type Item = T;

fn into_iter(mut self) -> Self::IntoIter {
Expand All @@ -458,7 +463,7 @@ impl<T: Enlist> IntoIterator for List<T> {
}
}

impl<T> Drop for ListIter<T> {
impl<T> Drop for ListIter<'_, T> {
fn drop(&mut self) {
if let List::Cons(head) = &mut self.list {
unsafe { destroy_list(head.list.as_ptr()) }
Expand Down

0 comments on commit d1ee368

Please sign in to comment.