Skip to content

Pass storage to constructors instead of returning value - #686

Open
lucic71 wants to merge 14 commits into
Cpp2Rust:masterfrom
lucic71:this-ptr
Open

lucic71 wants to merge 14 commits into
Cpp2Rust:masterfrom
lucic71:this-ptr

Conversation

@lucic71

@lucic71 lucic71 commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

Part of #674, closes #248

Returning values from the constructor does not follow the C++ semantics as shown in #674. This PR passes zero-initialised storage to the constructor instead.

I added a new trait called ZeroInitializable ([dcl.init.general]/6) which initialises all members to 0. In refcount it sets each member to 0, in unsafe it uses MaybeUninit::zeroed().assume_init().

Callers of constructors currently do:

let v = Rc::new(RefCell::new({
  let __v = Rc::new(RefCell::new(<T>::zero_init());
  T::new(__v.as_pointer(), ctor_args);
  Rc::try_unwrap(__v).ok().unwrap().into_inner()
}

This is not the final format, I will fix it in a future PR. I did not add here because the PR is already very big. The final format will be:

let v = Rc::new(RefCell::new(<T>::zero_init());
T::new(v.as_pointer(), ctor_args);

)*};
}

impl_zero_init!(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this map needed? You can use Default

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Self initialization doesn't work

2 participants