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

[stdlib] [NFC] Fix change UnsafePointer constructors to receive length as a UInt #3923

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions stdlib/src/builtin/string_literal.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ struct StringLiteral(
An iterator over the string.
"""
return _StringSliceIter[StaticConstantOrigin](
unsafe_pointer=self.unsafe_ptr(), length=self.byte_length()
ptr=self.unsafe_ptr(), length=self.byte_length()
)

fn __reversed__(self) -> _StringSliceIter[StaticConstantOrigin, False]:
Expand All @@ -450,7 +450,7 @@ struct StringLiteral(
A reversed iterator over the string.
"""
return _StringSliceIter[StaticConstantOrigin, False](
unsafe_pointer=self.unsafe_ptr(), length=self.byte_length()
ptr=self.unsafe_ptr(), length=self.byte_length()
)

fn __getitem__[IndexerType: Indexer](self, idx: IndexerType) -> String:
Expand Down
4 changes: 3 additions & 1 deletion stdlib/src/collections/list.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,9 @@ struct List[T: CollectionElement, hint_trivial_type: Bool = False](
for value in span:
self.append(value[])

fn __init__(mut self, *, ptr: UnsafePointer[T], length: Int, capacity: Int):
fn __init__(
out self, *, ptr: UnsafePointer[T], length: UInt, capacity: UInt
):
"""Constructs a list from a pointer, its length, and its capacity.

Args:
Expand Down
6 changes: 3 additions & 3 deletions stdlib/src/collections/string/string.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,7 @@ struct String(
self = literal.__str__()

@always_inline
fn __init__(out self, *, ptr: UnsafePointer[Byte], length: Int):
fn __init__(out self, *, ptr: UnsafePointer[Byte], length: UInt):
"""Creates a string from the buffer. Note that the string now owns
the buffer.

Expand Down Expand Up @@ -1344,7 +1344,7 @@ struct String(
An iterator of references to the string elements.
"""
return _StringSliceIter[__origin_of(self)](
unsafe_pointer=self.unsafe_ptr(), length=self.byte_length()
ptr=self.unsafe_ptr(), length=self.byte_length()
)

fn __reversed__(self) -> _StringSliceIter[__origin_of(self), False]:
Expand All @@ -1354,7 +1354,7 @@ struct String(
A reversed iterator of references to the string elements.
"""
return _StringSliceIter[__origin_of(self), forward=False](
unsafe_pointer=self.unsafe_ptr(), length=self.byte_length()
ptr=self.unsafe_ptr(), length=self.byte_length()
)

# ===------------------------------------------------------------------=== #
Expand Down
12 changes: 6 additions & 6 deletions stdlib/src/collections/string/string_slice.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,9 @@ struct _StringSliceIter[
var ptr: UnsafePointer[Byte]
var length: Int

fn __init__(mut self, *, unsafe_pointer: UnsafePointer[Byte], length: Int):
fn __init__(out self, *, ptr: UnsafePointer[Byte], length: UInt):
self.index = 0 if forward else length
self.ptr = unsafe_pointer
self.ptr = ptr
self.length = length

fn __iter__(self) -> Self:
Expand Down Expand Up @@ -320,7 +320,7 @@ struct StringSlice[mut: Bool, //, origin: Origin[mut]](
self = Self(unsafe_from_utf8=byte_slice)

@always_inline
fn __init__(out self, *, ptr: UnsafePointer[Byte], length: Int):
fn __init__(out self, *, ptr: UnsafePointer[Byte], length: UInt):
"""Construct a `StringSlice` from a pointer to a sequence of UTF-8
encoded bytes and a length.

Expand All @@ -334,7 +334,7 @@ struct StringSlice[mut: Bool, //, origin: Origin[mut]](
- `ptr` must point to data that is live for the duration of
`origin`.
"""
self._slice = Span[Byte, origin](ptr=ptr, length=length)
self = Self(unsafe_from_utf8=Span[Byte, origin](ptr=ptr, length=length))

@always_inline
fn copy(self) -> Self:
Expand Down Expand Up @@ -570,7 +570,7 @@ struct StringSlice[mut: Bool, //, origin: Origin[mut]](
An iterator of references to the string elements.
"""
return _StringSliceIter[origin](
unsafe_pointer=self.unsafe_ptr(), length=self.byte_length()
ptr=self.unsafe_ptr(), length=self.byte_length()
)

fn __reversed__(self) -> _StringSliceIter[origin, False]:
Expand All @@ -580,7 +580,7 @@ struct StringSlice[mut: Bool, //, origin: Origin[mut]](
A reversed iterator of references to the string elements.
"""
return _StringSliceIter[origin, forward=False](
unsafe_pointer=self.unsafe_ptr(), length=self.byte_length()
ptr=self.unsafe_ptr(), length=self.byte_length()
)

fn __getitem__[IndexerType: Indexer](self, idx: IndexerType) -> String:
Expand Down
2 changes: 1 addition & 1 deletion stdlib/src/memory/span.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ struct Span[
# ===------------------------------------------------------------------===#

@always_inline
fn __init__(out self, *, ptr: UnsafePointer[T], length: Int):
fn __init__(out self, *, ptr: UnsafePointer[T], length: UInt):
"""Unsafe construction from a pointer and length.

Args:
Expand Down
Loading