Skip to content

Add std::vector constructor overloads and tests - #52

Open
Ayuse wants to merge 4 commits into
rustfoundation:mainfrom
Ayuse:codex/vector-constructors
Open

Ayuse wants to merge 4 commits into
rustfoundation:mainfrom
Ayuse:codex/vector-constructors

Conversation

@Ayuse

@Ayuse Ayuse commented Oct 15, 2026 •

Copy link
Copy Markdown

Add C++ std::vector<int> constructors in Rust using the overload! macro.

  • Empty constructor: Creates a vector with no elements.
  • Count constructor: Creates a given number of elements, each set to zero.
  • Count-and-value constructor: Creates a given number of copies of a value.
  • Slice constructor: Copies elements from a Rust slice into a new C++ vector.
  1. Write Rust wrappers for that C++ type, for 3-5 different overloaded method names:
    • Create a new file named after your type in overloading-macros/cpp-overload-test/src/stdcpp
    • Wrap constructors first, writing tests to make sure the correct constructor is called
    • Explain any potential compatibility issues in a Limitations doc section
    • Explain usability issues in an Ergonomics doc section
    • Check that you have implemented all the overloads for each method name, or documented why you can't
    • Push your changes and wait for a review
  2. Add a separate example with some failing overloads
    • Create a new file named after your type in overloading-macros/cpp-overload-test/examples
    • Wrap accessor/modifier methods
    • Explain how they fail in an Incompatibilities doc section
    • Explain how the overload failures could be resolved in a Workarounds doc section
    • Optional: If there's a C++ feature with no equivalent in Rust, explain that in an Inexpressible Overloads doc section
    • Push your changes and wait for a review
  3. Update the overload macro compatibility table Markdown doc
    • If you haven't checked some kinds of overloads, put a ?in those squares
    • Push your changes and wait for a review

AI disclosure: Codex helped to explain and understand the problem, then gave conceptual solution.

@Ayuse Ayuse changed the title Add C++ vector constructor overload experiment Add std::vector constructor overloads and tests Oct 15, 2026
teor2345

This comment was marked as resolved.

@Ayuse
Ayuse marked this pull request as ready for review October 16, 2026 08:15

@teor2345 teor2345 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

Suggested change
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;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

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.

2 participants