From d274e919412ad887ed1e9580df7993d999b05ad5 Mon Sep 17 00:00:00 2001 From: "sashakoshka@tebibyte.media" Date: Tue, 10 Sep 2024 17:57:27 -0400 Subject: [PATCH] Add Cycler --- io/cycler.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 io/cycler.go 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) +} + +