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

CString behaves strangely with regard to *const and *mut #16772

Closed
wycats opened this issue Aug 26, 2014 · 5 comments
Closed

CString behaves strangely with regard to *const and *mut #16772

wycats opened this issue Aug 26, 2014 · 5 comments

Comments

@wycats
Copy link
Contributor

wycats commented Aug 26, 2014

In the current API, CString::new takes a *const i8, but it has a method as_mut_ptr which returns a *mut i8.

The as_mut_ptr method seems to violate the invariant that the new method is trying to impose.

Perhaps we need CString and MutCString, as well as MutCString::to_c_string?

@erickt
Copy link
Contributor

erickt commented Sep 22, 2014

Ack, that is pretty nasty. I think CString and MutCString sounds like a reasonable approach for this problem.

@huonw
Copy link
Member

huonw commented Sep 22, 2014

This is a problem with CVec too.

cc @aturon

(Parameterizable mutability would be another solution, I guess.)

@huonw huonw added the A-libs label Sep 22, 2014
@mzabaluev
Copy link
Contributor

Somewhat orthogonal to the mutability issue, I find the dynamic ownership flag too error-prone, considering that the main use case for CString is returning it by value and the result of doing that with the flag set to false would be a memory leak.

Perhaps CString and CMutString could be dedicated to wrapping allocated buffers, and there should be utilities for wrapping foreign-owned string pointers as &'a [u8] and constructing byte iterators over them.

@mzabaluev
Copy link
Contributor

Both .as_ptr() and .as_mut_ptr() are unsafe, so in principle, anything goes. But as far as I can see the use cases, CString is understood as an immutable wrapper, its main purpose being RAII over a foreign-allocated buffer while string modifications are preferably done elsewhere in Rust code. Perhaps there is no need for the crowbar that is .as_mut_ptr(), because one can always cast pointers explicitly in unsafe code and be aware of the implications.

mzabaluev added a commit to mzabaluev/rust-c-str that referenced this issue Nov 3, 2014
@alexcrichton
Copy link
Member

This was resolved in #20507

matthiaskrgr pushed a commit to matthiaskrgr/rust that referenced this issue Mar 10, 2024
internal: Add tracing spans to macro generated database
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

No branches or pull requests

5 participants