Skip to content

Commit

Permalink
[External] [stdlib] Make the use of the unsafe constructor of List ex…
Browse files Browse the repository at this point in the history
…plicit (#39465)

[External] [stdlib] Make the use of the unsafe constructor of List
explicit

This PR is a small piece of #2507

We want unsafe things to be explicit, as stated in [the vision
guide](https://github.com/modularml/mojo/blob/nightly/stdlib/docs/vision.md#objectives-and-goals)

Co-authored-by: Gabriel de Marmiesse <gabriel.demarmiesse@datadoghq.com>
Closes #2523
MODULAR_ORIG_COMMIT_REV_ID: 861433431c0fc7fd6ab1d905dfe09f3e3a5792c1
  • Loading branch information
Gabriel de Marmiesse authored and JoeLoser committed May 8, 2024
1 parent c749952 commit b0fd4f6
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
4 changes: 3 additions & 1 deletion stdlib/src/builtin/file.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,9 @@ struct FileHandle:
if err_msg:
raise (err_msg^).consume_as_error()

var list = List[Int8](buf, size=int(size_copy), capacity=int(size_copy))
var list = List[Int8](
unsafe_pointer=buf, size=int(size_copy), capacity=int(size_copy)
)

return list

Expand Down
12 changes: 8 additions & 4 deletions stdlib/src/builtin/string.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,9 @@ struct String(
var length = len(impl)
var capacity = impl.capacity
self._buffer = List[Int8](
impl.steal_data().bitcast[Int8](), size=length, capacity=capacity
unsafe_pointer=impl.steal_data().bitcast[Int8](),
size=length,
capacity=capacity,
)

@always_inline
Expand Down Expand Up @@ -574,7 +576,9 @@ 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(ptr, size=len, capacity=len))
self._buffer = Self._buffer_type(
unsafe_pointer=ptr, size=len, capacity=len
)

@always_inline
fn __init__(inout self, ptr: UnsafePointer[UInt8], len: Int):
Expand All @@ -589,8 +593,8 @@ 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(ptr.bitcast[Int8](), size=len, capacity=len)
self._buffer = Self._buffer_type(
unsafe_pointer=ptr.bitcast[Int8](), size=len, capacity=len
)

@always_inline
Expand Down
10 changes: 7 additions & 3 deletions stdlib/src/collections/list.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -128,16 +128,20 @@ struct List[T: CollectionElement](CollectionElement, Sized, Boolable):
self.append(value[])

fn __init__(
inout self: Self, data: UnsafePointer[T], *, size: Int, capacity: Int
inout self: Self,
*,
unsafe_pointer: UnsafePointer[T],
size: Int,
capacity: Int,
):
"""Constructs a list from a pointer, its size, and its capacity.
Args:
data: The pointer to the data.
unsafe_pointer: The pointer to the data.
size: The number of elements in the list.
capacity: The capacity of the list.
"""
self.data = data
self.data = unsafe_pointer
self.size = size
self.capacity = capacity

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 @@ -645,7 +645,7 @@ def test_constructor_from_pointer():
new_pointer[2] = 2
# rest is not initialized

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

0 comments on commit b0fd4f6

Please sign in to comment.