Repository navigation
Create storage in caller for by-value non-trivial arguments - #694
Merged
nunoplopes merged 15 commits intoDec 2, 2026
Merged
Conversation
nunoplopes
pushed a commit
that referenced
this pull request
Dec 4, 2026
Replace CppFrom with ConstructibleFrom and ConvertibleTo. Closes #700 ConstructibleFrom models `T t(args...)` and ConvertibleTo models `To t = from`. --- Each constructor now implements `Constructor<Args>`, where `Args` is the tuple of its parameter types: ```cpp struct S { S(const S &); S(NonTrivial, S *); S(NonTrivial, const S *); }; ``` ```rust impl Constructor<(LvalueRef<Const<S>>,)> for S { ... } // S(const S &) impl Constructor<(ParameterObject<NonTrivial>, Pointer<S>)> for S { ... } // S(NonTrivial, S *) impl Constructor<(ParameterObject<NonTrivial>, Pointer<Const<S>>)> for S { ... } // S(NonTrivial, const S *) ``` One `Constructor` impl per constructor means two constructors must not have the same Rust parameter types. Parameters use markers to distinguish between overloads: - `LvalueRef<T>`, `LvalueRef<Const<T>>`, etc (added in #699) - `Pointer<T>` and `Pointer<Const<T>>` for `T *` and `const T *`. I preferred a new Pointer marker instead of Ptr because `Ptr<Const<T>>` requires the slow reinterpret_cast to convert to `Ptr<T>` - `ParameterObject<T>` for a by-value argument passed as reference. The mechanism was added in #694, ParameterObject is added in this PR because otherwise it clashes with LvalueRef The special constructors are blanket impls over `Constructor`: - `DefaultConstructible`: `Constructor<()>` - `CopyConstructible`: `Constructor<(LvalueRef<Const<Self>>,)>` - `MoveConstructible`: `Constructor<(RvalueRef<Self>,)>` --- `Constructor<Args>` only matches exactly. `ConstructibleFrom` and `ConvertibleTo` must also do overload resolution, which Rust traits can't express. So both resolve overloads at run time (`libcc2rs/src/overload.rs`), using a macro called `Conversions` that the converter fills: ```rust #[derive(Conversions)] #[conversions(copy)] #[constructor(constructor_0, LvalueRef<Const<S>>)] #[constructor(constructor_1, ParameterObject<NonTrivial>, Pointer<S>)] #[constructor(constructor_2, ParameterObject<NonTrivial>, Pointer<Const<S>>)] #[conversion_function(to_i32, LvalueRef<Const<S>>, i32)] // operator int() const pub struct S { ... } ``` For example, the `vector::insert(pos, first, last)` rule mentioned in #700 constructs each element from `*it`, as C++ does: ```rust fn f101<T1, T3, T4>( o: Ptr<Vec<T1>>, pos: RandomAccessIterator<T1>, first: T3, last: T3, ) -> RandomAccessIterator<T1> where T1: ConstructibleFrom<(LvalueRef<Const<T4>>,)>, T3: CppInputIterator<T4>, { // for each element, from *it: ConstructibleFrom::construct( element, (LvalueRef::<Const<T4>>::bind(it.iter_deref()),), ); } ``` So these now behave as in C++: ```cpp struct Ratio { int num; operator int() const; }; struct Level { int raw; operator short() const; operator long() const; }; std::vector<long> longs; longs.insert(longs.end(), ratios, ratios + 2); // operator int, then int -> long std::vector<int> ints; ints.insert(ints.end(), levels, levels + 2); // operator short: short -> int is a promotion, // better than long -> int ``` --- The runtime overload resolution algorithm in libcc2rs is not complete. It mostly covers what is needed for the initialisation sequences of ConvertibleTo and ConstructibleFrom, some notable things that are missing and we can add later: * add deleted functions in the candidate list * prefer non-tempaltes functions over instantiations coming from templates * enum promotions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
For non-trivial by-value arguments, create the storage in the caller and pass a
Ptr<T>insted ofTto the function. Trivial by-value arguments continue to passT.Pass a
Ptr<T>instead ofValue<T>to the callee so that the caller continues having ownership. This is important later when we fix the destructor calls.