summaryrefslogtreecommitdiff
path: root/lib/diskio/file_state.go
blob: 63ec53b08289c1370a0647b0dfa96fd6585898a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Copyright (C) 2022  Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later

package diskio

type statefulFile[A ~int64] struct {
	inner File[A]
	pos   A
}

var _ File[assertAddr] = (*statefulFile[assertAddr])(nil)

func NewStatefulFile[A ~int64](file File[A]) *statefulFile[A] {
	return &statefulFile[A]{
		inner: file,
	}
}

func (sf *statefulFile[A]) Name() string                           { return sf.inner.Name() }
func (sf *statefulFile[A]) Size() A                                { return sf.inner.Size() }
func (sf *statefulFile[A]) Close() error                           { return sf.inner.Close() }
func (sf *statefulFile[A]) ReadAt(dat []byte, off A) (int, error)  { return sf.inner.ReadAt(dat, off) }
func (sf *statefulFile[A]) WriteAt(dat []byte, off A) (int, error) { return sf.inner.WriteAt(dat, off) }

func (sf *statefulFile[A]) Read(dat []byte) (n int, err error) {
	n, err = sf.ReadAt(dat, sf.pos)
	sf.pos += A(n)
	return n, err
}

func (sf *statefulFile[A]) ReadByte() (byte, error) {
	var dat [1]byte
	_, err := sf.Read(dat[:])
	return dat[0], err
}