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 List UnsafePointer constructor args #3742

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
2 changes: 1 addition & 1 deletion stdlib/src/builtin/file.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ struct FileHandle:
raise (err_msg^).consume_as_error()

var list = List[UInt8](
unsafe_pointer=buf, size=int(size_copy), capacity=int(size_copy)
ptr=buf, length=int(size_copy), capacity=int(size_copy)
)

return list
Expand Down
16 changes: 6 additions & 10 deletions stdlib/src/collections/list.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -175,21 +175,17 @@ struct List[T: CollectionElement, hint_trivial_type: Bool = False](
self.append(value[])

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

Args:
unsafe_pointer: The pointer to the data.
size: The number of elements in the list.
ptr: The pointer to the data.
length: The number of elements in the list.
capacity: The capacity of the list.
"""
self.data = unsafe_pointer
self.size = size
self.data = ptr
self.size = length
self.capacity = capacity

fn __moveinit__(inout self, owned existing: Self):
Expand Down
8 changes: 2 additions & 6 deletions stdlib/src/collections/string.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,7 @@ struct String(
var size = impl.size
var capacity = impl.capacity
self._buffer = Self._buffer_type(
unsafe_pointer=impl.steal_data(), size=size, capacity=capacity
ptr=impl.steal_data(), length=size, capacity=capacity
)

@always_inline
Expand Down Expand Up @@ -831,11 +831,7 @@ struct String(
"""
# we don't know the capacity of ptr, but we'll assume it's the same or
# larger than len
self = Self(
Self._buffer_type(
unsafe_pointer=ptr.bitcast[UInt8](), size=len, capacity=len
)
)
self = Self(Self._buffer_type(ptr=ptr, length=len, capacity=len))

# ===------------------------------------------------------------------=== #
# Factory dunders
Expand Down
4 changes: 2 additions & 2 deletions stdlib/src/utils/string_slice.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -1039,9 +1039,9 @@ fn _to_string_list[
og_ptr = unsafe_ptr_fn(i_ptr[i])
memcpy(p, og_ptr, og_len)
p[og_len] = 0 # null terminator
buf = String._buffer_type(unsafe_pointer=p, size=f_len, capacity=f_len)
buf = String._buffer_type(ptr=p, length=f_len, capacity=f_len)
(out_ptr + i).init_pointee_move(String(buf^))
return List[String](unsafe_pointer=out_ptr, size=i_len, capacity=i_len)
return List[String](ptr=out_ptr, length=i_len, capacity=i_len)


@always_inline
Expand Down
4 changes: 2 additions & 2 deletions stdlib/test/collections/test_list.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ def test_constructor_from_pointer():
new_pointer[2] = 2
# rest is not initialized

var some_list = List[Int8](unsafe_pointer=new_pointer, size=3, capacity=5)
var some_list = List[Int8](ptr=new_pointer, length=3, capacity=5)
assert_equal(some_list[0], 0)
assert_equal(some_list[1], 1)
assert_equal(some_list[2], 2)
Expand All @@ -731,7 +731,7 @@ def test_constructor_from_other_list_through_pointer():
var size = len(initial_list)
var capacity = initial_list.capacity
var some_list = List[Int8](
unsafe_pointer=initial_list.steal_data(), size=size, capacity=capacity
ptr=initial_list.steal_data(), length=size, capacity=capacity
)
assert_equal(some_list[0], 0)
assert_equal(some_list[1], 1)
Expand Down