struct Slice(T)
Overview
A Slice
is a Pointer
with an associated size.
While a pointer is unsafe because no bound checks are performed when reading from and writing to it,
reading from and writing to a slice involve bound checks.
In this way, a slice is a safe alternative to Pointer
.
A Slice can be created as read-only: trying to write to it
will raise. For example the slice of bytes returned by
String#to_slice
is read-only.
Included Modules
- Comparable(Slice(T))
- Indexable::Mutable(T)
Defined in:
ssz/codec.crssz/utils.cr
Class Method Summary
Instance Method Summary
-
#+(other : Slice(T)) : self
Concatenation.
-
#resize(new_size : Int32, value : T) : self
Resizes slice to
new_size
-size.
Instance methods inherited from module Enumerable(T)
hash_tree_root : Bytes
hash_tree_root,
ssz_basic? : Bool
ssz_basic?,
ssz_encode(io : IO)
ssz_encode,
ssz_size : Int32
ssz_size,
ssz_variable? : Bool
ssz_variable?
Class methods inherited from module Enumerable(T)
ssz_basic? : Bool
ssz_basic?
Instance methods inherited from class Object
ssz_basic? : Bool
ssz_basic?,
ssz_encode(io : IO)ssz_encode : Bytes ssz_encode, ssz_fixed? : Bool ssz_fixed?, ssz_size : Int32 ssz_size, ssz_variable? : Bool ssz_variable?
Class methods inherited from class Object
ssz_basic? : Bool
ssz_basic?,
ssz_decode(io : IO, size : Int32 = 0)ssz_decode(bytes : Bytes) ssz_decode, ssz_fixed? : Bool ssz_fixed?, ssz_variable? : Bool ssz_variable?
Class Method Detail
Instance Method Detail
Concatenation. Returns a new Slice(T)
built by concatenating self
and other.
The type of the new slice is the same of the type of original slices.
Bytes[1, 2] + Bytes[2, 3] # => Bytes[1, 2, 2, 3]
def resize(new_size : Int32, value : T) : self
#
Resizes slice to new_size
-size.
If new size if bigger than actual size sets new elemets to value
.
Bytes[1, 2].resize(4, 0_u8) # => Bytes[1, 2, 0, 0]
Bytes[1, 2, 3, 4].resize(2, 0_u8) # => Bytes[1, 2]