diff --git a/io/cycler.go b/io/cycler.go new file mode 100644 index 0000000..268f698 --- /dev/null +++ b/io/cycler.go @@ -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) +} + +