-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
[Feature Request] Add unsafe_ptr
and unsafe_cstr_ptr
to StringRef
and migrate the stdlib to use them
#3601
Comments
AFAIK we are migrating away from |
@martinvuyk Indeed, we should migrate most things to However, I suspected mojo/stdlib/src/python/_cpython.mojo Lines 847 to 851 in f4bc170
|
Couldn't we just add a
FWIW I'm quite aggressively trying to do just that in:
and the latest and most impactful
|
Yeah, we should probably do that as well.
This doesn't even have much to do with the available API. Moving away from In any case, I think migrating to |
If you mean interfacing with C then my answer is that you'd simply use fn PyImport_AddModule(inout self, name: StringSlice) -> PyObjectPtr:
var ptr = name.unsafe_cstr_ptr()
debug_assert(ptr[name.byte_length()] == 0, "slice must be null terminated")
var value = self.lib.get_function("PyImport_AddModule")(ptr)
return PyObjectPtr {value: value} and we still benefit from borrowed argument lifetime guarantees (what you taught me :D) |
It all technically can™ be done. It's just the said function is an internal function that I don't care to fix all the call-site right now. One problem though is that If you are interested, maybe you can look into having the methods on |
I'm totally up for changing it to |
I've been working on some ffi stuff in PR #3632 and I've introduced a helper function that I think makes more sense than adding public APIs to types that are used only for low level things (I'd actually like to deprecate any use of trait _UnsafePtrU8:
fn unsafe_ptr(self) -> UnsafePointer[UInt8]:
...
@always_inline
fn char_ptr[T: _UnsafePtrU8](item: T) -> UnsafePointer[c_char]:
"""Get the C.char pointer.
Parameters:
T: The type.
Args:
item: The item.
Returns:
The pointer.
"""
return item.unsafe_ptr().bitcast[c_char]()
@always_inline
fn char_ptr[T: AnyType](ptr: UnsafePointer[T]) -> UnsafePointer[c_char]:
"""Get the C.char pointer.
Parameters:
T: The type.
Args:
ptr: The pointer.
Returns:
The pointer.
"""
return ptr.bitcast[c_char]() |
@martinvuyk I like this approach. Could you make the change into its own PR (and maybe rename to |
Yeah I think you're right this will be better to separate to another PR, also on the name since this is in ffi I just copy pasted from my WIP c ffi package which I'd like to bring over to the stlib once we have the top level ffi package |
Review Mojo's priorities
What is your request?
As title. In the process, maybe also rename field
data
to_data
.What is your motivation for this change?
They are needed in e.g. Python interop.
Any other details?
N/A
The text was updated successfully, but these errors were encountered: