summaryrefslogtreecommitdiff
path: root/vendor/codeberg.org/gruf/go-iotools/read.go
blob: 13c5e21ac0b66868ddc99ecc73ca6d5a063c32c0 (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
37
38
39
40
41
package iotools

import (
	"io"
)

// ReadCloserType implements io.ReadCloser
// by combining the two underlying interfaces,
// while providing an exported type to still
// access the underlying original io.Reader or
// io.Closer separately (e.g. without wrapping).
type ReadCloserType struct {
	io.Reader
	io.Closer
}

// ReaderFunc is a function signature which allows
// a function to implement the io.Reader type.
type ReaderFunc func([]byte) (int, error)

func (r ReaderFunc) Read(b []byte) (int, error) {
	return r(b)
}

// ReaderFromFunc is a function signature which allows
// a function to implement the io.ReaderFrom type.
type ReaderFromFunc func(io.Reader) (int64, error)

func (rf ReaderFromFunc) ReadFrom(r io.Reader) (int64, error) {
	return rf(r)
}

// ReadCloser wraps an io.Reader and io.Closer in order to implement io.ReadCloser.
func ReadCloser(r io.Reader, c io.Closer) io.ReadCloser {
	return &ReadCloserType{r, c}
}

// NopReadCloser wraps io.Reader with NopCloser{} in ReadCloserType.
func NopReadCloser(r io.Reader) io.ReadCloser {
	return &ReadCloserType{r, NopCloser{}}
}