Skip to content

Commit

Permalink
Added ToByteArray trait
Browse files Browse the repository at this point in the history
This trait is used to convert a type to a ByteArray. Currently this
trait is implement for String and ByteArray.
  • Loading branch information
Yorick Peterse committed Jun 14, 2019
1 parent f818fe9 commit e357fd0
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion runtime/src/std/byte_array.inko
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ let ByteArray = _INKOC.get_byte_array_prototype

_INKOC.set_object_name(ByteArray, 'ByteArray')

## A type that can be converted to a `ByteArray`.
trait ToByteArray {
## Converts `self` to a `ByteArray`.
def to_byte_array -> ByteArray
}

impl ByteArray {
## Creates a new `ByteArray` from the given `Array` of `Integer` values.
##
Expand Down Expand Up @@ -400,7 +406,14 @@ impl Inspect for ByteArray {
}
}

impl String {
impl ToByteArray for ByteArray {
## Converts `self` to a `ByteArray`.
def to_byte_array -> ByteArray {
self
}
}

impl ToByteArray for String {
## Returns a `ByteArray` containing the bytes of this `String`.
def to_byte_array -> ByteArray {
_INKOC.string_to_byte_array(self) as ByteArray
Expand Down

0 comments on commit e357fd0

Please sign in to comment.