typedqueue

package module
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 17, 2023 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package typedqueue wraps a blobqueue, with serialization and runtime type checking of values.

Example
package main

import (
	"encoding/binary"
	"errors"
	"fmt"

	"github.com/blueboardio/go-blobqueue/memory"
	"github.com/blueboardio/go-blobqueue/typedqueue"
)

type Uint64 uint64

func (u Uint64) MarshalBinary() ([]byte, error) {
	b := make([]byte, binary.MaxVarintLen64)
	binary.PutUvarint(b, uint64(u))
	return b, nil
}

func (u *Uint64) UnmarshalBinary(b []byte) error {
	tmp, n := binary.Uvarint(b)
	if n <= 0 {
		return errors.New("invalid data")
	}
	*u = Uint64(tmp)
	return nil
}

func main() {
	q := typedqueue.New(&memory.Queue{}, Uint64(0))

	_ = q.Push(Uint64(1))
	_ = q.Push(Uint64(2))
	_ = q.Push(Uint64(3))

	li, _ := q.List()
	fmt.Printf("%#v\n", li)

}
Output:

[]typedqueue_test.Uint64{0x1, 0x2, 0x3}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Queue

type Queue struct {
	// contains filtered or unexported fields
}

func New

func New(q blobqueue.Queue, zeroValue encoding.BinaryMarshaler) *Queue

New wraps a queue to store any value of the same type that implements encoding.BinaryMarshaler and encoding.BinaryUnmarshaler.

Any marshaling/unmarshaling error is unexpected and raises a panic. So any error returned by the queue methods come from the original queue.

func (*Queue) Empty

func (q *Queue) Empty() error

Empty clears the queue.

func (*Queue) Len

func (q *Queue) Len() (int, error)

Len returns the length of the queue.

func (*Queue) List

func (q *Queue) List() (interface{}, error)

List returns all the items of the queue as a slice of values.

func (*Queue) Pop

func (q *Queue) Pop() (encoding.BinaryMarshaler, error)

Pop deletes the last element of the queue and returns this precise element. If the queue is empty it returns error blobqueue.ErrQueueIsEmpty.

func (*Queue) Push

func (q *Queue) Push(val encoding.BinaryMarshaler) error

Push appends the queue with a new elem (val).

func (*Queue) Shift

func (q *Queue) Shift() (encoding.BinaryMarshaler, error)

Shift deletes the first element of the queue and returns this precise element. If the queue is empty it returns error blobqueue.ErrQueueIsEmpty.

func (*Queue) Unshift

func (q *Queue) Unshift(val encoding.BinaryMarshaler) error

Unshift adds a new elem at the begining of the queue.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL