Skip to content

Add ConstructibleFrom and ConvertibleTo traits - #701

Merged
nunoplopes merged 37 commits into
Cpp2Rust:masterfrom
lucic71:constructible-from
Dec 4, 2026
Merged

nunoplopes merged 37 commits into
Cpp2Rust:masterfrom
lucic71:constructible-from

Conversation

@lucic71

@lucic71 lucic71 commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

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:

struct S {
  S(const S &);
  S(NonTrivial, S *);
  S(NonTrivial, const S *);
};
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:

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:

#[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:

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++:

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

CopyConstuctible and MoveConstructible can now be written in terms of
ConstructibleFrom.
# Conflicts:
#	cpp2rust/converter/converter.cpp
#	cpp2rust/converter/converter_lib.h
#	cpp2rust/converter/models/converter_refcount.cpp
#	libcc2rs-macros/src/constructor.rs
#	libcc2rs-macros/src/lib.rs
#	libcc2rs-macros/tests/ui/reference_binding.stderr
#	libcc2rs/src/capture.rs
#	libcc2rs/src/constructor.rs
#	libcc2rs/src/initialization.rs
#	libcc2rs/src/object.rs
#	libcc2rs/src/reference.rs
#	libcc2rs/src/swap.rs
#	rules/array/tgt_refcount.rs
#	rules/iterator_category/shim.rs
#	rules/map/shim.rs
#	rules/map/tgt_unsafe.rs
#	rules/pair/tgt_refcount.rs
#	rules/unique_ptr/shim.rs
#	rules/vector/shim.rs
#	tests/multi-file/defaulted_move_cross_tu/out/refcount/defaulted_move_cross_tu.rs
#	tests/multi-file/defaulted_move_cross_tu/out/unsafe/defaulted_move_cross_tu.rs
#	tests/multi-file/out_of_line_methods_cross_tu/out/refcount/out_of_line_methods_cross_tu.rs
#	tests/multi-file/out_of_line_methods_cross_tu/out/unsafe/out_of_line_methods_cross_tu.rs
#	tests/std/basic/basic.def.odr/p14-class-in-two-files/out/refcount/p14-class-in-two-files.rs
#	tests/std/basic/basic.def.odr/p14-class-in-two-files/out/unsafe/p14-class-in-two-files.rs
#	tests/std/basic/basic.def.odr/p15-class-template-static-member/out/refcount/p15-class-template-static-member.rs
#	tests/std/basic/basic.def.odr/p15-class-template-static-member/out/unsafe/p15-class-template-static-member.rs
#	tests/std/basic/basic.def.odr/p15-function-template/out/refcount/p15-function-template.rs
#	tests/std/basic/basic.def.odr/p15-function-template/out/unsafe/p15-function-template.rs
#	tests/std/basic/basic.def.odr/p15-inline-function/out/refcount/p15-inline-function.rs
#	tests/std/basic/basic.def.odr/p15-inline-function/out/unsafe/p15-inline-function.rs
#	tests/std/basic/basic.def.odr/p15-inline-variable/out/refcount/p15-inline-variable.rs
#	tests/std/basic/basic.def.odr/p15-inline-variable/out/unsafe/p15-inline-variable.rs
#	tests/std/basic/basic.def.odr/p16-default-argument-lambda-in-class/out/refcount/p16-default-argument-lambda-in-class.rs
#	tests/std/basic/basic.def.odr/p16-default-argument-lambda-in-class/out/unsafe/p16-default-argument-lambda-in-class.rs
#	tests/std/basic/basic.def.odr/p16-lambda-in-inline-function/out/refcount/p16-lambda-in-inline-function.rs
#	tests/std/basic/basic.def.odr/p16-lambda-in-inline-function/out/unsafe/p16-lambda-in-inline-function.rs
#	tests/std/basic/basic.scope/out/refcount/basic.scope.block.rs
#	tests/std/basic/basic.scope/out/unsafe/basic.scope.block.rs
#	tests/std/basic/out/refcount/basic.def.odr.rs
#	tests/std/basic/out/unsafe/basic.def.odr.rs
#	tests/std/class/class.mem/class.ctor/out/refcount/class.copy.ctor.rs
#	tests/std/class/class.mem/class.ctor/out/unsafe/class.copy.ctor.rs
#	tests/std/dcl.dcl/dcl.decl/dcl.init/out/refcount/dcl.init.ref.rs
#	tests/std/dcl.dcl/dcl.decl/dcl.init/out/unsafe/dcl.init.ref.rs
#	tests/std/expr/expr.compound/out/refcount/expr.mptr.oper.rs
#	tests/std/expr/expr.compound/out/unsafe/expr.mptr.oper.rs
#	tests/std/stmt.stmt/stmt.select/out/refcount/stmt.if.rs
#	tests/std/stmt.stmt/stmt.select/out/refcount/stmt.switch.rs
#	tests/std/stmt.stmt/stmt.select/out/unsafe/stmt.if.rs
#	tests/std/stmt.stmt/stmt.select/out/unsafe/stmt.switch.rs
#	tests/std/utilities/pairs/out/refcount/pairs.pair.rs
#	tests/std/utilities/pairs/out/unsafe/pairs.pair.rs
#	tests/std/utilities/utility/out/refcount/forward-helpers.rs
#	tests/std/utilities/utility/out/refcount/forward.rs
#	tests/std/utilities/utility/out/unsafe/forward-helpers.rs
#	tests/std/utilities/utility/out/unsafe/forward.rs
@nunoplopes
nunoplopes merged commit 0d1207f into Cpp2Rust:master Dec 4, 2026
8 of 9 checks passed
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.

Rules that convert between type parameters need overload resolution

2 participants