Skip to content

Commit

Permalink
polish
Browse files Browse the repository at this point in the history
Signed-off-by: martinvuyk <martin.vuyklop@gmail.com>
  • Loading branch information
martinvuyk committed Feb 28, 2025
1 parent ec3fa43 commit 5c6d9cc
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 49 deletions.
17 changes: 0 additions & 17 deletions mojo/stdlib/src/builtin/error.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -224,23 +224,6 @@ struct Error(
# Methods
# ===-------------------------------------------------------------------===#

@deprecated("Use `sys.ffi.c_str_ptr()` instead.")
fn unsafe_cstr_ptr(
ref self,
) -> UnsafePointer[
c_char,
mut = Origin(__origin_of(self)).is_mutable,
origin = __origin_of(self),
]:
"""Retrieves a C-string-compatible pointer to the underlying memory.
The returned pointer is guaranteed to be NUL terminated, and not null.
Returns:
The pointer to the underlying memory.
"""
return self.data.bitcast[c_char]()

@always_inline("nodebug")
fn unsafe_ptr(
ref self,
Expand Down
14 changes: 0 additions & 14 deletions mojo/stdlib/src/builtin/string_literal.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -520,20 +520,6 @@ struct StringLiteral(
# return type.
return ptr.bitcast[Byte]().origin_cast[False, StaticConstantOrigin]()

@deprecated("Use `sys.ffi.c_str_ptr()` instead.")
@always_inline
fn unsafe_cstr_ptr(
self,
) -> UnsafePointer[c_char, mut=False, origin=StaticConstantOrigin]:
"""Retrieves a C-string-compatible pointer to the underlying memory.
The returned pointer is guaranteed to be NUL terminated, and not null.
Returns:
The pointer to the underlying memory.
"""
return self.unsafe_ptr().bitcast[c_char]()

@always_inline
fn as_string_slice(self) -> StaticString:
"""Returns a string slice of this static string literal.
Expand Down
12 changes: 0 additions & 12 deletions mojo/stdlib/src/collections/string/string.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -1285,18 +1285,6 @@ struct String(
"""
return self._buffer.unsafe_ptr()

@deprecated("Use `sys.ffi.c_str_ptr()` instead.")
# FIXME(MSTDL-956): This should return a pointer with StaticConstantOrigin.
fn unsafe_cstr_ptr(self) -> UnsafePointer[c_char]:
"""Retrieves a C-string-compatible pointer to the underlying memory.
The returned pointer is guaranteed to be null, or NUL terminated.
Returns:
The pointer to the underlying memory.
"""
return self.unsafe_ptr().bitcast[c_char]()

@always_inline
fn as_bytes(ref self) -> Span[Byte, __origin_of(self)]:
"""Returns a contiguous slice of the bytes owned by this string.
Expand Down
44 changes: 38 additions & 6 deletions mojo/stdlib/src/sys/ffi.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ fn _c_long_long_dtype() -> DType:


@always_inline
fn c_str_ptr(item: StringLiteral) -> UnsafePointer[c_char]:
fn c_str_ptr(
item: StringLiteral,
) -> UnsafePointer[c_char, mut=False, origin=StaticConstantOrigin]:
"""Get the `c_char` pointer.
Args:
Expand All @@ -117,7 +119,13 @@ fn c_str_ptr(item: StringLiteral) -> UnsafePointer[c_char]:


@always_inline
fn c_str_ptr(item: Error) -> UnsafePointer[c_char]:
fn c_str_ptr(
ref item: Error,
) -> UnsafePointer[
c_char,
mut = Origin(__origin_of(item)).is_mutable,
origin = __origin_of(item),
]:
"""Get the `c_char` pointer.
Args:
Expand All @@ -130,7 +138,13 @@ fn c_str_ptr(item: Error) -> UnsafePointer[c_char]:


@always_inline
fn c_str_ptr(item: String) -> UnsafePointer[c_char]:
fn c_str_ptr(
ref item: String,
) -> UnsafePointer[
c_char,
mut = Origin(__origin_of(item)).is_mutable,
origin = __origin_of(item),
]:
"""Get the `c_char` pointer.
Args:
Expand All @@ -143,7 +157,11 @@ fn c_str_ptr(item: String) -> UnsafePointer[c_char]:


@always_inline
fn c_str_ptr(item: StringSlice) -> UnsafePointer[c_char]:
fn c_str_ptr(
item: StringSlice,
) -> UnsafePointer[
c_char, mut = __type_of(item).mut, origin = __type_of(item).origin
]:
"""Get the `c_char` pointer.
Args:
Expand All @@ -156,7 +174,13 @@ fn c_str_ptr(item: StringSlice) -> UnsafePointer[c_char]:


@always_inline
fn c_str_ptr[T: CollectionElement](item: List[T]) -> UnsafePointer[c_char]:
fn c_str_ptr[
T: CollectionElement
](item: List[T]) -> UnsafePointer[
c_char,
mut = Origin(__origin_of(item)).is_mutable,
origin = __origin_of(item),
]:
"""Get the `c_char` pointer.
Parameters:
Expand All @@ -172,7 +196,15 @@ fn c_str_ptr[T: CollectionElement](item: List[T]) -> UnsafePointer[c_char]:


@always_inline
fn c_str_ptr[T: CollectionElement](item: Span[T]) -> UnsafePointer[c_char]:
fn c_str_ptr[
T: CollectionElement
](item: Span[T]) -> UnsafePointer[
c_char,
mut = __type_of(item).mut,
origin = __type_of(item).origin,
address_space = __type_of(item).address_space,
alignment = __type_of(item).alignment,
]:
"""Get the `c_char` pointer.
Parameters:
Expand Down

0 comments on commit 5c6d9cc

Please sign in to comment.