Repository navigation
Conversation
teor2345
left a comment
There was a problem hiding this comment.
It looks like you're missing some overload sets (we want 3-5 different function names), and some overloads within those sets (we want all of the possible overloads).
Please focus on completing this PR so you make it through all 3 steps before the application period ends.
| //! | ||
| //! ### Limitations | ||
| //! | ||
| //! This experiment fixes the element type to C++ `int`; it does not test generic overloads. |
There was a problem hiding this comment.
It would be clearer to use a Rust type alias T and C++ typedef item_t. This also makes it easier to change the type to see if overloads conflict with other types.
See the forward_list example for how this can be done.
| //! ### Limitations | ||
| //! | ||
| //! This experiment fixes the element type to C++ `int`; it does not test generic overloads. | ||
| //! C++ allocation exceptions are not translated into Rust errors. |
There was a problem hiding this comment.
Please research how C++ shows allocation errors, and what happens at the FFI boundary to those errors, and document it here.
| }} | ||
|
|
||
| /// Owns a C++ `std::vector<int>`; its pointer stays private so callers cannot replace it. | ||
| pub struct StdVector(*mut c_void); |
There was a problem hiding this comment.
Rust requires a PhamtomData<T> in this struct, to show that it contains instances of type T behind the pointer. This makes sure that auto-traits are implemented correctly.
| pub struct StdVector(*mut c_void); | |
| pub struct StdVector(*mut c_void, PhantomData<T>); |
| // An empty Rust slice may have a dangling pointer. Use a real C++ object | ||
| // for its empty iterator range, avoiding arithmetic on that pointer. | ||
| const int empty = 0; | ||
| const int* first = count == 0 ? &empty : items; |
There was a problem hiding this comment.
We want to eventually make this code generic. Would it work for Rust zero-sized types (ZSTs)?
Please research how they work in Rust and C++, and document the limitations of this implementation. If you want you can instead add an implementation and a test with ZSTs, but that could be a lot trickier.
| // not invoke the C++ move constructor. | ||
| // - Initializer-list: needs a distinct input representation; the slice overload | ||
| // calls the iterator-pair constructor instead. | ||
| // - C++23 from_range: needs a range bridge and a distinct tag or argument type. |
There was a problem hiding this comment.
Please add as many of these constructors as is reasonable, or document why they can't be added. We need to implement the full overload set to show they are all compatible with each other.
See the forward_list example for details.
Add C++
std::vector<int>constructors in Rust using theoverload!macro.Limitationsdoc sectionErgonomicsdoc sectionIncompatibilitiesdoc sectionWorkaroundsdoc sectionInexpressible Overloadsdoc sectionAI disclosure: Codex helped to explain and understand the problem, then gave conceptual solution.