From f62c9990bd1a60e330597ab062e80417c40cb1e7 Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Tue, 5 Nov 2024 12:22:27 -0300 Subject: [PATCH] rename List UnsafePointer constructor args Signed-off-by: martinvuyk --- stdlib/src/builtin/file.mojo | 2 +- stdlib/src/collections/list.mojo | 16 ++++++---------- stdlib/src/collections/string.mojo | 8 ++------ stdlib/src/utils/string_slice.mojo | 4 ++-- stdlib/test/collections/test_list.mojo | 4 ++-- 5 files changed, 13 insertions(+), 21 deletions(-) diff --git a/stdlib/src/builtin/file.mojo b/stdlib/src/builtin/file.mojo index 15afa6606a..8db5c42927 100644 --- a/stdlib/src/builtin/file.mojo +++ b/stdlib/src/builtin/file.mojo @@ -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 diff --git a/stdlib/src/collections/list.mojo b/stdlib/src/collections/list.mojo index 1587298703..f4ab245419 100644 --- a/stdlib/src/collections/list.mojo +++ b/stdlib/src/collections/list.mojo @@ -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): diff --git a/stdlib/src/collections/string.mojo b/stdlib/src/collections/string.mojo index 3f9e28aa47..a4a616a833 100644 --- a/stdlib/src/collections/string.mojo +++ b/stdlib/src/collections/string.mojo @@ -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 @@ -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 diff --git a/stdlib/src/utils/string_slice.mojo b/stdlib/src/utils/string_slice.mojo index 54d72b4bd7..bcbd71e9fb 100644 --- a/stdlib/src/utils/string_slice.mojo +++ b/stdlib/src/utils/string_slice.mojo @@ -1053,9 +1053,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 diff --git a/stdlib/test/collections/test_list.mojo b/stdlib/test/collections/test_list.mojo index 96b88b8a83..35365af488 100644 --- a/stdlib/test/collections/test_list.mojo +++ b/stdlib/test/collections/test_list.mojo @@ -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) @@ -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)