Skip to content

Commit

Permalink
[stdlib] Add trait CollectionElementNew to a few structs
Browse files Browse the repository at this point in the history
Signed-off-by: gabrieldemarmiesse <[email protected]>
  • Loading branch information
gabrieldemarmiesse committed Jun 28, 2024
1 parent 439d86d commit ad0bc0e
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
13 changes: 12 additions & 1 deletion stdlib/src/builtin/dtype.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ alias _mIsFloat = UInt8(1 << 6)

@value
@register_passable("trivial")
struct DType(Stringable, Formattable, Representable, KeyElement):
struct DType(
Stringable, Formattable, Representable, KeyElement, CollectionElementNew
):
"""Represents DType and provides methods for working with it."""

alias type = __mlir_type.`!kgen.dtype`
Expand Down Expand Up @@ -83,6 +85,15 @@ struct DType(Stringable, Formattable, Representable, KeyElement):
of the hardware's pointer type (32-bit on 32-bit machines and 64-bit on
64-bit machines)."""

@always_inline
fn __init__(inout self, *, other: Self):
"""Copy this DType.
Args:
other: The DType to copy.
"""
self = other

@always_inline("nodebug")
fn __str__(self) -> String:
"""Gets the name of the DType.
Expand Down
20 changes: 19 additions & 1 deletion stdlib/src/builtin/error.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@ from memory.memory import _free


@register_passable
struct Error(Stringable, Boolable, Representable, Formattable):
struct Error(
Stringable,
Boolable,
Representable,
Formattable,
CollectionElement,
CollectionElementNew,
):
"""This type represents an Error."""

var data: UnsafePointer[UInt8]
Expand Down Expand Up @@ -104,6 +111,17 @@ struct Error(Stringable, Boolable, Representable, Formattable):
dest[length] = 0
return Error {data: dest, loaded_length: -length}

fn __init__(*, other: Self) -> Self:
"""Copy the object.
Args:
other: The value to copy.
Returns:
The copied `Error`.
"""
return other

fn __del__(owned self):
"""Releases memory if allocated."""
if self.loaded_length < 0:
Expand Down
14 changes: 12 additions & 2 deletions stdlib/src/builtin/object.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ from utils import StringRef, Variant, unroll


@register_passable("trivial")
struct _NoneMarker:
struct _NoneMarker(CollectionElementNew):
"""This is a trivial class to indicate that an object is `None`."""

pass
fn __init__(inout self, *, other: Self):
pass


@register_passable("trivial")
Expand Down Expand Up @@ -316,6 +317,15 @@ struct _ObjectImpl(CollectionElement, Stringable):
fn __init__(inout self, value: _RefCountedAttrsDictRef):
self.value = Self.type(value)

@always_inline
fn __init__(inout self, *, other: Self):
"""Copy the object.
Args:
other: The value to copy.
"""
self = other.value

@always_inline
fn __copyinit__(inout self, existing: Self):
self = existing.value
Expand Down
10 changes: 10 additions & 0 deletions stdlib/src/builtin/string_literal.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ struct StringLiteral(
Boolable,
Formattable,
Comparable,
CollectionElementNew,
):
"""This type represents a string literal.
Expand Down Expand Up @@ -68,6 +69,15 @@ struct StringLiteral(
"""
self.value = value

@always_inline("nodebug")
fn __init__(inout self, *, other: Self):
"""Copy constructor.
Args:
other: The string literal to copy.
"""
self = other

# ===-------------------------------------------------------------------===#
# Operator dunders
# ===-------------------------------------------------------------------===#
Expand Down

0 comments on commit ad0bc0e

Please sign in to comment.