Repository navigation
Add ConstructibleFrom and ConvertibleTo traits - #701
Merged
Merged
Conversation
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
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.
Replace CppFrom with ConstructibleFrom and ConvertibleTo. Closes #700
ConstructibleFrom models
T t(args...)and ConvertibleTo modelsTo t = from.Each constructor now implements
Constructor<Args>, whereArgsis the tuple of its parameter types:One
Constructorimpl 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 Add value category and cv qualifiers markers #699)Pointer<T>andPointer<Const<T>>forT *andconst T *. I preferred a new Pointer marker instead of Ptr becausePtr<Const<T>>requires the slow reinterpret_cast to convert toPtr<T>ParameterObject<T>for a by-value argument passed as reference. The mechanism was added in Create storage in caller for by-value non-trivial arguments #694, ParameterObject is added in this PR because otherwise it clashes with LvalueRefThe special constructors are blanket impls over
Constructor:DefaultConstructible:Constructor<()>CopyConstructible:Constructor<(LvalueRef<Const<Self>>,)>MoveConstructible:Constructor<(RvalueRef<Self>,)>Constructor<Args>only matches exactly.ConstructibleFromandConvertibleTomust also do overload resolution, which Rust traits can't express. So both resolve overloads at run time (libcc2rs/src/overload.rs), using a macro calledConversionsthat the converter fills:For example, the
vector::insert(pos, first, last)rule mentioned in #700 constructs each element from*it, as C++ does:So these now behave as in C++:
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: