Skip to content

Commit

Permalink
Fix moved-in-memory problem.
Browse files Browse the repository at this point in the history
Partially fixes #833.
  • Loading branch information
adetaylor committed Mar 1, 2022
1 parent 0da4d89 commit dade678
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
6 changes: 4 additions & 2 deletions engine/src/conversion/codegen_rs/function_wrapper_rs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl TypeConversionPolicy {
panic!("Unexpected non-ident parameter name");
};
let space_var_name = make_ident(format!("{}_space", var_name));
let call = quote! { autocxx::ValueParamHandler::new(#var_name) };
let call = quote! { #space_var_name.populate() };
let call = if wrap_in_unsafe {
quote! {
unsafe {
Expand All @@ -121,7 +121,9 @@ impl TypeConversionPolicy {
};
(
Some(quote! {
let mut #space_var_name = #call;
let mut #space_var_name = autocxx::ValueParamHandler::new(#var_name);
let #space_var_name = &mut #space_var_name;
#call
}),
quote! {
#space_var_name.get_ptr()
Expand Down
21 changes: 14 additions & 7 deletions src/value_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,24 +162,31 @@ impl<T, VP: ValueParam<T>> ValueParamHandler<T, VP> {
/// this may be largely a no-op or it may involve storing a whole
/// extra copy of the type.
///
/// # Safety
///
/// Callers must guarantee that this type will not move
/// in memory.
pub unsafe fn new(param: VP) -> Self {
pub fn new(param: VP) -> Self {
let mut this = Self {
param,
space: None,
_pinned: PhantomPinned,
};
if this.param.needs_stack_space() {
this.space = Some(MaybeUninit::uninit());
this.param
.populate_stack_space(Pin::new_unchecked(this.space.as_mut().unwrap()));
}
this
}

/// Populate this stack space if needs be.
///
/// # Safety
///
/// Callers must guarantee that this type will not move
/// in memory between calls to [`populate`] and [`get_ptr`].
pub unsafe fn populate(&mut self) {
if self.param.needs_stack_space() {
self.param
.populate_stack_space(Pin::new_unchecked(self.space.as_mut().unwrap()));
}
}

/// Return a pointer to the underlying value which can be passed to C++.
/// Per the unsafety contract of `new`, the object must not have moved
/// since it was created.
Expand Down

0 comments on commit dade678

Please sign in to comment.