# `Yex.Buffer`
[🔗](https://github.com/satoren/y_ex/blob/main/lib/buffer.ex#L1)

Bytes stored as a buffer value: a `Uint8Array` to a Yjs client.

Elixir has one type for text and bytes, so a bare binary is stored as a
string when it is valid UTF-8 and as a buffer only when it is not. Wrap the
bytes in a `Yex.Buffer` to store them as a buffer whatever they contain, in
a map, an array, an XML attribute or a prelim.

A buffer is always read back as a `Yex.Buffer`, so a value that is read and
written again keeps its type.

A buffer in a text's formatting attributes or in a text embed is not kept as
a buffer: Yjs stores those as JSON, so after a sync the bytes read back as a
list of numbers, both in another `Yex.Doc` and in a Yjs client. Only the
document that wrote it reads it as a `Yex.Buffer` until then.

## Examples
    iex> doc = Yex.Doc.new()
    iex> map = Yex.Doc.get_map(doc, "map")
    iex> Yex.Map.set(map, "image", Yex.Buffer.from("RIFF"))
    iex> Yex.Map.fetch(map, "image")
    {:ok, %Yex.Buffer{data: "RIFF"}}
    iex> Yex.Map.set(map, "bytes", <<0xFF, 0xD8>>)
    iex> Yex.Map.fetch(map, "bytes")
    {:ok, %Yex.Buffer{data: <<0xFF, 0xD8>>}}
    iex> Yex.Map.set(map, "text", "RIFF")
    iex> Yex.Map.fetch(map, "text")
    {:ok, "RIFF"}

# `t`

```elixir
@type t() :: %Yex.Buffer{data: binary()}
```

# `from`

```elixir
@spec from(binary()) :: t()
```

Wraps `data`, the bytes to store as a buffer.

## Examples
    iex> Yex.Buffer.from(<<0xFF, 0xD8>>)
    %Yex.Buffer{data: <<0xFF, 0xD8>>}

---

*Consult [api-reference.md](api-reference.md) for complete listing*
