portald/warp/rw.go

38 lines
688 B
Go
Raw Normal View History

2023-06-07 17:43:58 -05:00
package warp
import (
"io"
"github.com/portal-co/portald/floppy"
)
type RW struct {
Reader io.Reader
Writer io.Writer
}
func (r RW) Read(p []byte) (n int, err error) {
return r.Reader.Read(p)
}
func (r RW) Write(p []byte) (n int, err error) {
return r.Writer.Write(p)
}
func XF2X(f func(io.ReadWriter) error) io.ReadWriter {
ra, wa := io.Pipe()
rb, wb := io.Pipe()
go func() {
err := f(RW{Reader: ra, Writer: wb})
if err == nil {
err = io.EOF
}
ra.CloseWithError(err)
wb.CloseWithError(err)
}()
return RW{Reader: rb, Writer: wa}
}
func X2XF(x io.ReadWriter) func(io.ReadWriter) error {
return func(rw io.ReadWriter) error {
return floppy.Floppy(x, rw)
}
}