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] Use UnsafePointer in vector.mojo #2377

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 9 additions & 9 deletions stdlib/src/collections/vector.mojo
Expand Up @@ -19,7 +19,7 @@ from collections.vector import InlinedFixedVector
```
"""

from memory import Pointer, Reference
from memory import UnsafePointer, Reference

from utils import StaticTuple

Expand All @@ -31,14 +31,14 @@ from utils import StaticTuple
@value
struct _VecIter[
type: AnyRegType,
vec_type: AnyRegType,
deref: fn (Pointer[vec_type], Int) -> type,
vec_type: AnyType,
deref: fn (UnsafePointer[vec_type], Int) -> type,
](Sized):
"""Iterator for any random-access container"""

var i: Int
var size: Int
var vec: Pointer[vec_type]
var vec: UnsafePointer[vec_type]

fn __next__(inout self) -> type:
self.i += 1
Expand Down Expand Up @@ -100,7 +100,7 @@ struct InlinedFixedVector[
alias static_data_type = StaticTuple[type, size]
var static_data: Self.static_data_type
"""The underlying static storage, used for small vectors."""
var dynamic_data: Pointer[type]
var dynamic_data: UnsafePointer[type]
"""The underlying dynamic storage, used to grow large vectors."""
var current_size: Int
"""The number of elements in the vector."""
Expand All @@ -117,9 +117,9 @@ struct InlinedFixedVector[
capacity: The requested maximum capacity of the vector.
"""
self.static_data = Self.static_data_type() # Undef initialization
self.dynamic_data = Pointer[type]()
self.dynamic_data = UnsafePointer[type]()
if capacity > Self.static_size:
self.dynamic_data = Pointer[type].alloc(capacity - size)
self.dynamic_data = UnsafePointer[type].alloc(capacity - size)
self.current_size = 0
self.capacity = capacity

Expand Down Expand Up @@ -231,7 +231,7 @@ struct InlinedFixedVector[
self.current_size = 0

@staticmethod
fn _deref_iter_impl(selfptr: Pointer[Self], i: Int) -> type:
fn _deref_iter_impl(selfptr: UnsafePointer[Self], i: Int, /) -> type:
return selfptr[][i]

alias _iterator = _VecIter[type, Self, Self._deref_iter_impl]
Expand All @@ -243,5 +243,5 @@ struct InlinedFixedVector[
An iterator to the start of the vector.
"""
return Self._iterator(
0, self.current_size, Reference(self).get_legacy_pointer()
0, self.current_size, UnsafePointer(Reference(self))
Copy link
Contributor

@soraros soraros May 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe:

Suggested change
0, self.current_size, UnsafePointer(Reference(self))
0, self.current_size, UnsafePointer.address_of(self)

)