Add Cycler

This commit is contained in:
Sasha Koshka 2024-09-10 17:57:27 -04:00
parent 9655da5841
commit d274e91941

28
io/cycler.go Normal file
View File

@ -0,0 +1,28 @@
package uio
import "io"
// Cycler stores any io.Closer. When the value is replaced, the old value is
// closed.
type Cycler struct {
value io.Closer
}
// Value returns the value of the Cycler. If there is no value, nil is returned.
func (this *Cycler) Value () io.Closer {
return this.value
}
// Set replaces the value of the Cycler, closing the previous one if it exists.
func (this *Cycler) Set (value io.Closer) error {
err := this.value.Close()
this.value = value
return err
}
// Close closes the Cycler's value early. It will be set to nil.
func (this *Cycler) Close () error {
return this.Set(nil)
}