Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace BnStrCompatible with AsCStr trait #5897

Open
wants to merge 1 commit into
base: rust_break_everything
Choose a base branch
from

Conversation

mkrasnitski
Copy link
Contributor

@mkrasnitski mkrasnitski commented Sep 2, 2024

This is a rather large PR, but the essence of the changes is in string.rs. Replaces the BnStrCompatible trait with an AsCStr trait with the following definition:

pub trait AsCStr {
    fn as_cstr(&self) -> Cow<'_, CStr>;
}

Note that this trait borrows &self instead of consuming self. This simplifies the list of implementing types and reduces the need for cloning. The as_cstr method returns a Cow in order to provide implementations both for already null-terminated strings (BnString) and for non-terminated strings (&str/String).

Note that it is still possible in some cases to create a pointer to a temporary that gets immediately dropped if calling s.as_cstr().as_ptr() inline, same as with BnStrCompatible. I was careful to avoid doing so when swapping out the traits.

@CouleeApps CouleeApps added the Component: Rust API Issue needs changes to the Rust API label Oct 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Rust API Issue needs changes to the Rust API
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants