struct Slice(T)
- Slice(T)
- Struct
- Value
- Object
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:
remilib/extensions.crInstance Method Summary
-
#realloc(newSize : Int32, default : T) : self
Similar to
#realloc()
in C. -
#toArray : Array(T)
Returns a new
Array
that contains the same elements as this slice.
Instance Method Detail
Similar to #realloc()
in C. This returns a new Slice
with the new
requested size. The contents of self
are copied over to the new Slice
.
If the new Slice
is larger than self
, then the new elements are set to
default
. If the new Slice
is smaller, then only as many items as will
fit are copied over.
Internally, this is equivalent to either using Slice#[range]
when the new
size is smaller, Slice#dup
when the sizes are the same, and
Slice#initialize
+ Slice#copy_from
when the new size is larger.
Returns a new Array
that contains the same elements as this slice.