Skip to content

Add U32string_view wrapper - #71

Open
Jennycj wants to merge 3 commits into
rustfoundation:mainfrom
Jennycj:u32string-view-overloads
Open

Jennycj wants to merge 3 commits into
rustfoundation:mainfrom
Jennycj:u32string-view-overloads

Conversation

@Jennycj

@Jennycj Jennycj commented Nov 4, 2026 •

Copy link
Copy Markdown

Added a wrapper for std::U32string_view with 4 overloads including constructor, and methods: compare, find and startwith
Implements #43

  1. Write Rust wrappers for that U32string_view, 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

@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.

Hi, thank you for this PR!

Please submit steps 2 & 3 immediately, without waiting for reviews. Outreachy applications are due on Monday 9 November, and I want you to be able to get one or two reviews of everything in your PR.

Please remember to submit an Outreachy application! You can include any Rust work during the application period.

I added a merge commit, feel free to keep it or force-push over it.

//!
//! Callers can only build a view from static data, such as string-literal constants, constant
//! arrays, or a leaked `Box`. A view of a local array is a compile error. Supporting borrowed
//! data needs generics (and lifetime) support in `overload!`.

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 move all this documentation next to the types or functions it documents. That way, when we fix these issues, we will see the documentation and update it.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Noted, I will do this.

///
/// `std::basic_string_view<CharT>` is a template, so this wrapper is hardcoded to `char32_t`.
#[cfg_attr(not(test), expect(dead_code, reason = "Only used in tests"))]
pub struct StdU32StringView(*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 marker type to show what this type contains behind the pointer.

Suggested change
pub struct StdU32StringView(*mut c_void);
pub struct StdU32StringView(*mut c_void, PhantomData<CharT>);

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks for this suggestion, I will apply it.

#[cfg_attr(not(test), expect(dead_code, reason = "Only used in tests"))]
pub struct StdU32StringView(*mut c_void);

/// The C++ `char32_t` type is used as `u32` in Rust since it has the same size and alignment.

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.

Unfortunately, this is not guaranteed, even if most C++ platforms do it that way:
https://en.cppreference.com/c/string/multibyte/char32_t

This is a portability limitation, which could result in a compiler failure on weird platforms (the cpp! macro checks size and alignment match). Please update the docs here, document this limitation, and link to the reference.

It is true Rust does not have a corresponding type in its standard library, but it could in future.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks fpr pointing this out.
I'll update the documentation to explain that u32 is the Rust-side representation, but compatibility with C++ char32_t depends on the target platform.
I will also document the limitation and add the reference link.

impl<'a> NullTerminated<'a> {
/// Returns `None` if `s` has no zero terminator, because C++ would read past the end.
pub fn new(s: &'a [CharT]) -> Option<NullTerminated<'a>> {
s.contains(&0).then_some(NullTerminated(s))

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.

What happens if the null terminator is not at the end of the slice?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Characters after the null terminator will be ignored even if they are not at the end of the slice since the C++ constructor stops reading at the first null terminator.

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.

This might be worth documenting?

StdU32StringView(view)
}

fn copy_from(other: &StdU32StringView) -> StdU32StringView {

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.

This constructor is only sound because all the other constructors take static data. Otherwise, it could extend the lifetime of the new string view beyond the lifetime of its contents.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I agree.
The copy constructor only copies the std::u32string_view and does not extend the lifetime of the underlying character storage.

The current implementation relies on all constructors restricting the underlying character data to 'static, which makes this safe. I'll document this invariant alongside the copy constructor.

//! Wrapped overloaded names: constructors, `compare`, `find`, `starts_with`.
//!
//! The `basic_string_view(nullptr_t) = delete` constructor (C++23) is not wrapped.
//! It exists only to reject `nullptr` at compile time, and Rust raw pointers

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.

Suggested change
//! It exists only to reject `nullptr` at compile time, and Rust raw pointers
//! It exists only to reject `nullptr` at compile time, and Rust references

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Noted, thank you!

@Jennycj

Jennycj commented Nov 5, 2026

Copy link
Copy Markdown
Author

Hi, thank you for this PR!

Please submit steps 2 & 3 immediately, without waiting for reviews. Outreachy applications are due on Monday 9 November, and I want you to be able to get one or two reviews of everything in your PR.

Please remember to submit an Outreachy application! You can include any Rust work during the application period.

I added a merge commit, feel free to keep it or force-push over it.

Thank you @teor2345 for the reminder!
I will keep the merge commit and send in 2 & 3 as soon as possible

@teor2345

teor2345 commented Nov 6, 2026

Copy link
Copy Markdown
Collaborator

Just reminding you that the Outreachy final application deadline is this Monday 9 November. You can include any Rust PR or ticket work you've done, it doesn't have to be merged (or reviewed) yet.

Please submit a final application on the Outreachy website before Monday. The exact deadline is 4pm UTC, but it is better to submit early. You can edit your submitted application up until the deadline (and you can keep working on PRs after the deadline).

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