Skip to content

Commit

Permalink
[External] [stdlib] Add CollectionElementNew to a few structs, part 4…
Browse files Browse the repository at this point in the history
… (#42564)

[External] [stdlib] Add CollectionElementNew to a few structs, part 4

Adding the explicit copy constructor is needed for when collections will
require all their elements to have an explicit copy constructor.

This PR contains the modification for `_ImmutableString`, `_ObjectImpl`,
`_FormatCurlyEntry`, `OwnedKwargsDict` and `_NoneType`

Co-authored-by: Gabriel de Marmiesse <[email protected]>
Closes #3143
MODULAR_ORIG_COMMIT_REV_ID: 4722182a83f5a0ae701dd7d57999591fda9fb087
  • Loading branch information
Gabriel de Marmiesse authored and modularbot committed Jul 1, 2024
1 parent 805dbe9 commit b728910
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
8 changes: 6 additions & 2 deletions stdlib/src/builtin/object.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ struct _NoneMarker(CollectionElementNew):


@register_passable("trivial")
struct _ImmutableString:
struct _ImmutableString(CollectionElement, CollectionElementNew):
"""Python strings are immutable. This class is marked as trivially register
passable because its memory will be managed by `_ObjectImpl`. It is a
pointer and integer pair. Memory will be dynamically allocated.
Expand All @@ -53,6 +53,10 @@ struct _ImmutableString:
self.data = data
self.length = length

@always_inline
fn __init__(inout self, *, other: Self):
self = other

@always_inline
fn string_compare(self, rhs: _ImmutableString) -> Int:
var res = memcmp(self.data, rhs.data, min(self.length, rhs.length))
Expand Down Expand Up @@ -234,7 +238,7 @@ struct _Function:
)


struct _ObjectImpl(CollectionElement, Stringable):
struct _ObjectImpl(CollectionElement, CollectionElementNew, Stringable):
"""This class is the underlying implementation of the value of an `object`.
It is a variant of primitive types and pointers to implementations of more
complex types.
Expand Down
10 changes: 8 additions & 2 deletions stdlib/src/builtin/string.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -2431,7 +2431,7 @@ fn _calc_format_buffer_size[type: DType]() -> Int:


@value
struct _FormatCurlyEntry:
struct _FormatCurlyEntry(CollectionElement, CollectionElementNew):
"""
Internally used by the `format()` method.
Expand All @@ -2447,14 +2447,20 @@ struct _FormatCurlyEntry:
var last_curly: Int
"""The index of an closing brace around a substitution field."""

var field: Variant[
alias _FieldVariantType = Variant[
String, # kwargs indexing (`{field_name}`)
Int, # args manual indexing (`{3}`)
NoneType, # args automatic indexing (`{}`)
Bool, # for escaped curlies ('{{')
]
var field: Self._FieldVariantType
"""Store the substitution field."""

fn __init__(inout self, *, other: Self):
self.first_curly = other.first_curly
self.last_curly = other.last_curly
self.field = Self._FieldVariantType(other=other.field)

fn is_escaped_brace(ref [_]self) -> Bool:
return self.field.isa[Bool]()

Expand Down
12 changes: 11 additions & 1 deletion stdlib/src/collections/dict.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -986,7 +986,9 @@ struct Dict[K: KeyElement, V: CollectionElement](
self._n_entries = self.size


struct OwnedKwargsDict[V: CollectionElement](Sized, CollectionElement):
struct OwnedKwargsDict[V: CollectionElement](
Sized, CollectionElement, CollectionElementNew
):
"""Container used to pass owned variadic keyword arguments to functions.
This type mimics the interface of a dictionary with `String` keys, and
Expand All @@ -1010,6 +1012,14 @@ struct OwnedKwargsDict[V: CollectionElement](Sized, CollectionElement):
"""Initialize an empty keyword dictionary."""
self._dict = Dict[Self.key_type, V]()

fn __init__(inout self, *, other: Self):
"""Copy an existing keyword dictionary.
Args:
other: The existing keyword dictionary.
"""
self._dict = other._dict

fn __copyinit__(inout self, existing: Self):
"""Copy an existing keyword dictionary.
Expand Down
5 changes: 3 additions & 2 deletions stdlib/src/collections/optional.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ from utils import Variant

# TODO(27780): NoneType can't currently conform to traits
@value
struct _NoneType(CollectionElement):
pass
struct _NoneType(CollectionElement, CollectionElementNew):
fn __init__(inout self, *, other: Self):
pass


# ===----------------------------------------------------------------------===#
Expand Down

0 comments on commit b728910

Please sign in to comment.