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] Rename Span UnsafePointer constructor args #3743

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
12 changes: 5 additions & 7 deletions stdlib/src/builtin/sort.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ fn _quicksort[
var imm_interval = stack.pop()
var ptr = imm_interval.unsafe_ptr()
var len = len(imm_interval)
var interval = Span[type, origin](unsafe_ptr=ptr, len=len)
var interval = Span[type, origin](ptr=ptr, length=len)

if len <= 5:
_delegate_small_sort[cmp_fn](interval)
Expand All @@ -242,19 +242,17 @@ fn _quicksort[
var pivot = _quicksort_partition_left[cmp_fn](interval)
if len > pivot + 2:
stack.append(
ImmSpan(unsafe_ptr=ptr + pivot + 1, len=len - pivot - 1)
ImmSpan(ptr=ptr + pivot + 1, length=len - pivot - 1)
)
continue

var pivot = _quicksort_partition_right[cmp_fn](interval)

if len > pivot + 2:
stack.append(
ImmSpan(unsafe_ptr=ptr + pivot + 1, len=len - pivot - 1)
)
stack.append(ImmSpan(ptr=ptr + pivot + 1, length=len - pivot - 1))

if pivot > 1:
stack.append(ImmSpan(unsafe_ptr=ptr, len=pivot))
stack.append(ImmSpan(ptr=ptr, length=pivot))


# ===----------------------------------------------------------------------===#
Expand Down Expand Up @@ -357,7 +355,7 @@ fn _stable_sort[
](span: Span[type, origin]):
var temp_buff = UnsafePointer[type].alloc(len(span))
var temp_buff_span = Span[type, __origin_of(temp_buff)](
unsafe_ptr=temp_buff, len=len(span)
ptr=temp_buff, length=len(span)
)
_stable_sort_impl[cmp_fn](span, temp_buff_span)
temp_buff.free()
Expand Down
6 changes: 2 additions & 4 deletions stdlib/src/builtin/string_literal.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -456,8 +456,7 @@ struct StringLiteral(
"""

return Span[Byte, StaticConstantOrigin](
unsafe_ptr=self.unsafe_ptr(),
len=self.byte_length(),
ptr=self.unsafe_ptr(), length=self.byte_length()
)

@always_inline
Expand All @@ -472,8 +471,7 @@ struct StringLiteral(
"""
# Does NOT include the NUL terminator.
return Span[Byte, __origin_of(self)](
unsafe_ptr=self.unsafe_ptr(),
len=self.byte_length(),
ptr=self.unsafe_ptr(), length=self.byte_length()
)

@always_inline
Expand Down
2 changes: 1 addition & 1 deletion stdlib/src/collections/string.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -1533,7 +1533,7 @@ struct String(

# Does NOT include the NUL terminator.
return Span[Byte, __origin_of(self)](
unsafe_ptr=self._buffer.unsafe_ptr(), len=self.byte_length()
ptr=self._buffer.unsafe_ptr(), length=self.byte_length()
)

@always_inline
Expand Down
8 changes: 2 additions & 6 deletions stdlib/src/utils/inline_string.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,7 @@ struct InlineString(Sized, Stringable, CollectionElement, CollectionElementNew):
"""

return Span[Byte, __origin_of(self)](
unsafe_ptr=self.unsafe_ptr(),
# Does NOT include the NUL terminator.
len=len(self),
ptr=self.unsafe_ptr(), length=len(self)
)


Expand Down Expand Up @@ -532,7 +530,5 @@ struct _FixedString[CAP: Int](
"""

return Span[Byte, __origin_of(self)](
unsafe_ptr=self.unsafe_ptr(),
# Does NOT include the NUL terminator.
len=self.size,
ptr=self.unsafe_ptr(), length=self.size
)
15 changes: 7 additions & 8 deletions stdlib/src/utils/span.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,15 @@ struct Span[
# ===------------------------------------------------------------------===#

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

Args:
unsafe_ptr: The underlying pointer of the span.
len: The length of the view.
ptr: The underlying pointer of the span.
length: The length of the view.
"""
self._data = unsafe_ptr
self._len = len
self._data = ptr
self._len = length

@always_inline
fn __init__(inout self, *, other: Self):
Expand Down Expand Up @@ -202,8 +202,7 @@ struct Span[
step == 1, "Slice must be within bounds and step must be 1"
)
var res = Self(
unsafe_ptr=(self._data + start),
len=len(range(start, end, step)),
ptr=(self._data + start), length=len(range(start, end, step))
)

return res
Expand Down Expand Up @@ -351,5 +350,5 @@ struct Span[
A span covering the same elements, but without mutability.
"""
return Span[T, _lit_mut_cast[origin, False].result](
unsafe_ptr=self._data, len=self._len
ptr=self._data, length=self._len
)
10 changes: 5 additions & 5 deletions stdlib/src/utils/string_slice.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ struct _StringSliceIter[
self.ptr = unsafe_pointer
self.length = length
alias S = Span[Byte, StaticConstantOrigin]
var s = S(unsafe_ptr=self.ptr, len=self.length)
var s = S(ptr=self.ptr, length=self.length)
self.continuation_bytes = _count_utf8_continuation_bytes(s)

fn __iter__(self) -> Self:
Expand Down Expand Up @@ -316,8 +316,8 @@ struct StringSlice[is_mutable: Bool, //, origin: Origin[is_mutable].type,](
var strref = unsafe_from_utf8_strref

var byte_slice = Span[Byte, origin](
unsafe_ptr=strref.unsafe_ptr(),
len=len(strref),
ptr=strref.unsafe_ptr(),
length=len(strref),
)

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

@always_inline
fn __init__(inout self, *, other: Self):
Expand Down Expand Up @@ -386,7 +386,7 @@ struct StringSlice[is_mutable: Bool, //, origin: Origin[is_mutable].type,](
"""
var b_len = self.byte_length()
alias S = Span[Byte, StaticConstantOrigin]
var s = S(unsafe_ptr=self.unsafe_ptr(), len=b_len)
var s = S(ptr=self.unsafe_ptr(), length=b_len)
return b_len - _count_utf8_continuation_bytes(s)

fn write_to[W: Writer](self, inout writer: W):
Expand Down
6 changes: 2 additions & 4 deletions stdlib/src/utils/stringref.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,7 @@ struct StringRef(
Returns:
A contiguous slice pointing to the bytes owned by this string.
"""
return Span[Byte, __origin_of(self)](
unsafe_ptr=self.data, len=self.length
)
return Span[Byte, __origin_of(self)](ptr=self.data, length=self.length)

# ===-------------------------------------------------------------------===#
# Operator dunders
Expand Down Expand Up @@ -413,7 +411,7 @@ struct StringRef(
# Safe because our use of this Span does not outlive `self`.
writer.write_bytes(
Span[Byte, ImmutableAnyOrigin](
unsafe_ptr=self.unsafe_ptr(), len=len(self)
ptr=self.unsafe_ptr(), length=len(self)
)
)

Expand Down
2 changes: 1 addition & 1 deletion stdlib/src/utils/write.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ struct _WriteBuffer[W: MovableWriter, //, capacity: Int](Writer):
fn flush(inout self):
self.writer.write_bytes(
Span[Byte, ImmutableAnyOrigin](
unsafe_ptr=self.data.unsafe_ptr(), len=self.pos
ptr=self.data.unsafe_ptr(), length=self.pos
)
)
self.pos = 0
Expand Down
2 changes: 1 addition & 1 deletion stdlib/test/builtin/test_sort_issue_1018.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ from utils import Span
fn sort_test[D: DType, name: StringLiteral](size: Int, max: Int) raises:
var p = UnsafePointer[SIMD[D, 1]].alloc(size)
rand[D](p, size)
sort(Span[Scalar[D], MutableAnyOrigin](unsafe_ptr=p, len=size))
sort(Span[Scalar[D], MutableAnyOrigin](ptr=p, length=size))
for i in range(1, size - 1):
if p[i] < p[i - 1]:
print(name, "size:", size, "max:", max, "incorrect sort")
Expand Down
2 changes: 1 addition & 1 deletion stdlib/test/utils/test_string_slice.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ def test_count_utf8_continuation_bytes():

def _test(amnt: Int, items: List[UInt8]):
p = items.unsafe_ptr()
span = Span[Byte, StaticConstantOrigin](unsafe_ptr=p, len=len(items))
span = Span[Byte, StaticConstantOrigin](ptr=p, length=len(items))
assert_equal(amnt, _count_utf8_continuation_bytes(span))

_test(5, List[UInt8](c, c, c, c, c))
Expand Down