# Slice from UInt8

**URL:** <https://forum.crystal-lang.org/t/slice-from-uint8/2071>\
**Category:** Help & Support\
**Created:** [May 6, 2020, 6:00am UTC](https://forum.crystal-lang.org/t/slice-from-uint8/2071 "2020-05-06T06:00:15Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Didactic.Drunk](https://avatars.discourse-cdn.com/v4/letter/d/eada6e/32.png) [@Didactic.Drunk](https://forum.crystal-lang.org/u/Didactic.Drunk)\
**Post date:** [May 6, 2020, 6:00am UTC](https://forum.crystal-lang.org/t/slice-from-uint8/2071/1 "2020-05-06T06:00:15Z")

</div>

Can I get a `Slice` pointing at a single `UInt8` without creating a new `Bytes` or `StaticArray`?

---

<div class="post-metadata">

**Author:** ![jhass](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jhass/32/1779_2.png) [@jhass](https://forum.crystal-lang.org/u/jhass)\
**Post date:** [May 6, 2020, 7:25am UTC](https://forum.crystal-lang.org/t/slice-from-uint8/2071/2 "2020-05-06T07:25:04Z")

</div>

`Bytes` is defined as just `alias Bytes = Slice(UInt8)`. With this in mind your question could be rephrased to “Can I get a `Slice` … without creating a new `Slice` …?” :)

So the answer is obviously no, just do `Bytes[10u8]`.

However maybe for your usecase you can avoid allocating the slice by not using a slice altogether? Always good to mention the underlying problem for which you seek a solution, rather than how to realize a solution you already came up with :)

---

<div class="post-metadata">

**Author:** ![Exilor](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/exilor/32/780_2.png) [@Exilor](https://forum.crystal-lang.org/u/Exilor)\
**Post date:** [May 6, 2020, 8:25am UTC](https://forum.crystal-lang.org/t/slice-from-uint8/2071/3 "2020-05-06T08:25:41Z")

</div>

This might be what you want:

```
x = 0u8
slice = pointerof(x).to_slice(1)
slice[0] = 1u8
p slice # => Bytes[1]

```

It doesn’t allocate anything on the heap.

---

<div class="post-metadata">

**Author:** ![Didactic.Drunk](https://avatars.discourse-cdn.com/v4/letter/d/eada6e/32.png) [@Didactic.Drunk](https://forum.crystal-lang.org/u/Didactic.Drunk)\
**Post date:** [May 6, 2020, 3:10pm UTC](https://forum.crystal-lang.org/t/slice-from-uint8/2071/4 "2020-05-06T15:10:51Z")

</div>

> [@jhass](#):
>
> `Bytes[10u8]`

I really want to avoid heap allocation and `Bytes.new`.

`Bytes[]` calls `T.slice`

```auto
  # The slice is allocated on the heap.
  macro slice(*nums, read_only = false)

```

---

<div class="post-metadata">

**Author:** ![jhass](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jhass/32/1779_2.png) [@jhass](https://forum.crystal-lang.org/u/jhass)\
**Post date:** [May 6, 2020, 3:39pm UTC](https://forum.crystal-lang.org/t/slice-from-uint8/2071/5 "2020-05-06T15:39:30Z")

</div>

Well you got your solution above :) Maybe there’s a safe and more idiomatic one though, one that doesn’t even need a slice…

---

<div class="post-metadata">

**Author:** ![Didactic.Drunk](https://avatars.discourse-cdn.com/v4/letter/d/eada6e/32.png) [@Didactic.Drunk](https://forum.crystal-lang.org/u/Didactic.Drunk)\
**Post date:** [May 6, 2020, 3:42pm UTC](https://forum.crystal-lang.org/t/slice-from-uint8/2071/6 "2020-05-06T15:42:21Z")

</div>

How would you handle passing portions of c structs to `IO` methods, `Digest#update` and other stdlib methods that only take a `Slice`?

---

<div class="post-metadata">

**Author:** ![jhass](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jhass/32/1779_2.png) [@jhass](https://forum.crystal-lang.org/u/jhass)\
**Post date:** [May 6, 2020, 3:48pm UTC](https://forum.crystal-lang.org/t/slice-from-uint8/2071/7 "2020-05-06T15:48:23Z")

</div>

For `IO` there’s the `write_bytes` interface which is supported by all primitive types in stdlib, through the `to_io` interface defined on them. One could expand on that by defining `to_io` methods for compound types.

If something is type restricted to a `Slice` argument with no alternative API, of course you need to get your hands onto a slice somehow :)

In general I still think providing the context on where you need something is still beneficial to the question quality and allows to offer solutions outside of the path you might have imagined so far. That’s all I was hinting at :)

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [May 6, 2020, 3:55pm UTC](https://forum.crystal-lang.org/t/slice-from-uint8/2071/8 "2020-05-06T15:55:03Z")

</div>

@Didactic.Drunk can you provide some sample code of what you are trying to do?

---

<div class="post-metadata">

**Author:** ![Didactic.Drunk](https://avatars.discourse-cdn.com/v4/letter/d/eada6e/32.png) [@Didactic.Drunk](https://forum.crystal-lang.org/u/Didactic.Drunk)\
**Post date:** [May 6, 2020, 5:34pm UTC](https://forum.crystal-lang.org/t/slice-from-uint8/2071/9 "2020-05-06T17:34:28Z")

</div>

@asterite Porting existing code from ruby/c.  
@exilor’s answer seems to work but I haven’t validated the hash values yet.

```auto
slice = pointerof(struct.member).to_slice(1)
digest.update slice

```

Memory allocations using `Bytes.new(1)` were killing performance. I could keep a persistent `Bytes` allocated but a `Slice` directly to the `UInt8` seems like a better option.

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [May 6, 2020, 5:49pm UTC](https://forum.crystal-lang.org/t/slice-from-uint8/2071/10 "2020-05-06T17:49:06Z")

</div>

Using pointerof is fine as long as you don’t give that pointer to something that keeps a reference to it.

Also see this: [https://github.com/crystal-lang/crystal/blob/2cbe65b0a13c8a2ce91bd4799f5b96b8e1953097/src/io.cr#L842-L845](https://github.com/crystal-lang/crystal/blob/2cbe65b0a13c8a2ce91bd4799f5b96b8e1953097/src/io.cr#L842-L845)

```crystal
  def write_byte(byte : UInt8)
    x = byte
    write Slice.new(pointerof(x), 1)
  end

```

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [May 6, 2020, 9:01pm UTC](https://forum.crystal-lang.org/t/slice-from-uint8/2071/11 "2020-05-06T21:01:38Z")

</div>

I’d also point out that `StaticArray` doesn’t allocate on the heap, so it might actually be a viable, more high-level solution where you don’t need to work with pointers directly.

---

<div class="post-metadata">

**Author:** ![Didactic.Drunk](https://avatars.discourse-cdn.com/v4/letter/d/eada6e/32.png) [@Didactic.Drunk](https://forum.crystal-lang.org/u/Didactic.Drunk)\
**Post date:** [May 13, 2020, 3:21pm UTC](https://forum.crystal-lang.org/t/slice-from-uint8/2071/12 "2020-05-13T15:21:11Z")

</div>

In the process of making a PR adding `write(byte : UInt8)` to `Digest` I wondered if it made more sense to add `to_slice` to `UInt8`?

Thoughts?

---

<div class="post-metadata">

**Author:** ![jhass](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jhass/32/1779_2.png) [@jhass](https://forum.crystal-lang.org/u/jhass)\
**Post date:** [May 13, 2020, 3:24pm UTC](https://forum.crystal-lang.org/t/slice-from-uint8/2071/13 "2020-05-13T15:24:18Z")

</div>

Isn’t that what `DigestIO` is for? So it gets methods like `IO#write_byte` inherited for free?

---

<div class="post-metadata">

**Author:** ![Didactic.Drunk](https://avatars.discourse-cdn.com/v4/letter/d/eada6e/32.png) [@Didactic.Drunk](https://forum.crystal-lang.org/u/Didactic.Drunk)\
**Post date:** [May 13, 2020, 11:39pm UTC](https://forum.crystal-lang.org/t/slice-from-uint8/2071/14 "2020-05-13T23:39:33Z")

</div>

I don’t think so. DigestIO calculates a `Digest` of the unmodified stream as you read/write an `IO`. I don’t have an `IO` and if it was it’s not the entire stream or in order.

> I wondered if it made more sense to add `to_slice` to `UInt8` ?

The comment above is about adding easy `UInt8` compatibility for every function that accepts `Bytes` which is more than `IO`.

---

<div class="post-metadata">

**Author:** ![jhass](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jhass/32/1779_2.png) [@jhass](https://forum.crystal-lang.org/u/jhass)\
**Post date:** [May 14, 2020, 7:31am UTC](https://forum.crystal-lang.org/t/slice-from-uint8/2071/15 "2020-05-14T07:31:07Z")

</div>

I think it’s fair if doing this is a bit hard, `IO#write_byte(s)` is just so much more sane of an interface and we should push people towards it where possible. I don’t want people to end up with `io.write 1u8.to_slice`.

---

<div class="post-metadata">

**Author:** ![Didactic.Drunk](https://avatars.discourse-cdn.com/v4/letter/d/eada6e/32.png) [@Didactic.Drunk](https://forum.crystal-lang.org/u/Didactic.Drunk)\
**Post date:** [May 14, 2020, 3:48pm UTC](https://forum.crystal-lang.org/t/slice-from-uint8/2071/16 "2020-05-14T15:48:05Z")

</div>

I think `write` takes anything that responds to `#to_slice` as does `Digest#update` and many more.

What I propose:

```auto
io.write 1_u8
digest.update 2_u8
cipher.update 3_u8
any_duck_typed_to_slice_accepting_method 4_u8

```

---

<div class="post-metadata">

**Author:** ![Didactic.Drunk](https://avatars.discourse-cdn.com/v4/letter/d/eada6e/32.png) [@Didactic.Drunk](https://forum.crystal-lang.org/u/Didactic.Drunk)\
**Post date:** [May 14, 2020, 3:49pm UTC](https://forum.crystal-lang.org/t/slice-from-uint8/2071/17 "2020-05-14T15:49:56Z")

</div>

If it’s not done at the `UInt8` level, code must be repeated for every function that wants to accept a `Slice | Byte`

---

<div class="post-metadata">

**Author:** ![jhass](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jhass/32/1779_2.png) [@jhass](https://forum.crystal-lang.org/u/jhass)\
**Post date:** [May 14, 2020, 3:53pm UTC](https://forum.crystal-lang.org/t/slice-from-uint8/2071/18 "2020-05-14T15:53:19Z")

</div>

Maybe `DigestIO` is a misnomer and Should be `DigestIOWrapper` or something less terrible, and then `Digest` should just include `IO` and disable `read`?

We have this wonderful `to_io` interface, it would indeed be a shame to not use it.

---

<div class="post-metadata">

**Author:** ![jhass](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jhass/32/1779_2.png) [@jhass](https://forum.crystal-lang.org/u/jhass)\
**Post date:** [May 14, 2020, 4:00pm UTC](https://forum.crystal-lang.org/t/slice-from-uint8/2071/19 "2020-05-14T16:00:45Z")

</div>

Maybe `Digest` should even use a builder API akin to `String.build`, yielding an `IO`. Having to call `final` on it always felt like an odd API to me.

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [May 14, 2020, 4:22pm UTC](https://forum.crystal-lang.org/t/slice-from-uint8/2071/20 "2020-05-14T16:22:48Z")

</div>

There’s `write_byte` for IO. I think having it for Digest is good too.

`UInt8#to_slice` will **never** happen because it’s unsafe. If you retain the slice and pass it around, it might crash your program or cause undefined behavior.

[Next page](https://forum.crystal-lang.org/t/slice-from-uint8/2071.md?page=2)
