Skip to content

Commit

Permalink
[External] [stdlib] Use UnsafePointer in vector.mojo (#38804)
Browse files Browse the repository at this point in the history
[External] [stdlib] Use `UnsafePointer` in `vector.mojo`

As part of the migrating from `Pointer` to `UnsafePointer`,
continue this in `vector.mojo`.  This requires changing one bit
to use `AnyType` rather than `AnyRegType` to play nicely
with `UnsafePointer`.  In the near future, we want to replace
use of `AnyRegType` with `AnyType` throughout the library.

---------

Co-authored-by: gabrieldemarmiesse <gabrieldemarmiesse@gmail.com>
Closes #2377
MODULAR_ORIG_COMMIT_REV_ID: a9d3d17ea2dc2e092569829d632754c7c81a6a39
  • Loading branch information
gabrieldemarmiesse authored and JoeLoser committed May 6, 2024
1 parent 344b2ed commit 1815d30
Showing 1 changed file with 9 additions and 9 deletions.
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))
)

0 comments on commit 1815d30

Please sign in to comment.