-
-
Notifications
You must be signed in to change notification settings - Fork 240
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add MemCx<'mcx>
for List<'mcx, T>
#1342
Add MemCx<'mcx>
for List<'mcx, T>
#1342
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
UX thoughts: I probably didn't write the interfaces correctly. Might need redesign.
Wondered about maybe instead using something like List<T, M>
, where M
might be the thing infected with the lifetime? idk.
pgrx/src/pg_sys.rs
Outdated
mem::current_context(|cx| { | ||
crate::list::List::<*mut core::ffi::c_void>::downcast_ptr_in_memcx(range_table, cx) | ||
.expect("rt_fetch used on non-ptr List") | ||
.get((index - 1) as _) | ||
.expect("rt_fetch used out-of-bounds") | ||
.cast() | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This diff suggests to me my approach is kinda incorrect, since I don't really "use" the context.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, this is probably going to be the most annoying part here. Maybe this would be better captured as another parameter to List, so non-allocating but owning uses don't have to worry about this.
pgrx/src/mem.rs
Outdated
/// Acquire the current context and operate inside it. | ||
pub fn current_context<'curr, F, T>(f: F) -> T | ||
where | ||
F: for<'clos> FnOnce(&'clos MemCx<'curr>) -> T, | ||
{ | ||
let memcx = unsafe { MemCx::from_ptr(pg_sys::CurrentMemoryContext) }; | ||
let ret = { f(&memcx) }; | ||
ret | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definitely not sure about soundness, but something similar to this is probably required, for ergonomics reasons.
iter: RawCellIter<T>, | ||
} | ||
|
||
/// A list being drained. | ||
#[derive(Debug)] | ||
pub struct Drain<'a, T> { | ||
pub struct Drain<'a, 'cx, T> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really need to move Drain itself "up".
Also: two lifetimes! spooky.
Just a note to say I've skimmed over this. I don't have any feedback yet other than it's exciting to see some code demonstrating the ideas. |
pgrx/src/pg_sys.rs
Outdated
mem::current_context(|cx| { | ||
crate::list::List::<*mut core::ffi::c_void>::downcast_ptr_in_memcx(range_table, cx) | ||
.expect("rt_fetch used on non-ptr List") | ||
.get((index - 1) as _) | ||
.expect("rt_fetch used out-of-bounds") | ||
.cast() | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, this is probably going to be the most annoying part here. Maybe this would be better captured as another parameter to List, so non-allocating but owning uses don't have to worry about this.
8c42257
to
d1ee368
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a good improvement.
d1ee368
to
dd422ae
Compare
Rebased and added some (~300 words) of documentation on memory contexts. I think I will land this now, so that it's easier to build off of and mutate as-we-go. |
MemCx<'mcx>
with List<'mcx, T>
MemCx<'mcx>
with List<'mcx, T>
MemCx<'mcx>
for List<'mcx, T>
List requires reallocation in order to push to it with safe Rust, but it is defined as allocated via palloc, which has problems with lifetimes. In order to head off these problems, and begin work on related ones, introduce a type describing a MemoryContext with a lifetime, including scoped (therefore lifetime-bound) access to the current context.