Compare commits
7 Commits
6bfa97e6aa
...
v0.5.0
| Author | SHA1 | Date | |
|---|---|---|---|
| 9fd40a37b8 | |||
| 709f41a974 | |||
| f40eca261b | |||
| e5b35f3fcc | |||
| e6a94c487e | |||
| 0f903cc8ec | |||
| 19fcdb4a7d |
@@ -1,31 +1,31 @@
|
|||||||
package ucontainer
|
package ucontainer
|
||||||
|
|
||||||
// Optional is an optional value.
|
// Optional can either hold a value, or nothing.
|
||||||
type Optional[T any] struct {
|
type Optional[T any] struct {
|
||||||
value T
|
value T
|
||||||
exists bool
|
exists bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// Value returns the value and true if the value exists. If not, it returns the
|
// O creates a new optional with the specified value.
|
||||||
// last set value and false.
|
func O[T any] (value T) Optional[T] {
|
||||||
func (this *Optional[T]) Value () (T, bool) {
|
return Optional[T] {
|
||||||
return this.value, this.exists
|
value: value,
|
||||||
|
exists: true,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set sets the value.
|
// Void returns an optional with no value.
|
||||||
func (this *Optional[T]) Set (value T) {
|
func Void[T any] () Optional[T] {
|
||||||
this.value = value
|
return Optional[T] { }
|
||||||
this.exists = true
|
|
||||||
}
|
|
||||||
|
|
||||||
// Unset unsets the value.
|
|
||||||
func (this *Optional[T]) Unset () {
|
|
||||||
var zero T
|
|
||||||
this.value = zero
|
|
||||||
this.exists = false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Exists returns if the value is currently set.
|
// Exists returns if the value is currently set.
|
||||||
func (this *Optional[T]) Exists () bool {
|
func (optional Optional[T]) Exists () bool {
|
||||||
return this.exists
|
return optional.exists
|
||||||
|
}
|
||||||
|
|
||||||
|
// Value returns the value and true if the value exists. If not, it returns the
|
||||||
|
// zero value and false.
|
||||||
|
func (optional Optional[T]) Value () (T, bool) {
|
||||||
|
return optional.value, optional.exists
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,15 @@ package ucontainer
|
|||||||
// Set is a set of unique items, built on top of map.
|
// Set is a set of unique items, built on top of map.
|
||||||
type Set[T comparable] map[T] struct { }
|
type Set[T comparable] map[T] struct { }
|
||||||
|
|
||||||
|
// NewSet creates a new set that contains all specified items.
|
||||||
|
func NewSet[T comparable] (items ...T) Set[T] {
|
||||||
|
set := make(Set[T])
|
||||||
|
for _, item := range items {
|
||||||
|
set.Add(item)
|
||||||
|
}
|
||||||
|
return set
|
||||||
|
}
|
||||||
|
|
||||||
// Empty returns true if there are no items in the set.
|
// Empty returns true if there are no items in the set.
|
||||||
func (set Set[T]) Empty () bool {
|
func (set Set[T]) Empty () bool {
|
||||||
return set == nil || len(set) == 0
|
return set == nil || len(set) == 0
|
||||||
|
|||||||
2
go.mod
2
go.mod
@@ -1,3 +1,3 @@
|
|||||||
module git.tebibyte.media/sashakoshka/goutil
|
module git.tebibyte.media/sashakoshka/goutil
|
||||||
|
|
||||||
go 1.20.0
|
go 1.22.0
|
||||||
|
|||||||
@@ -1,6 +1,23 @@
|
|||||||
package ucolor
|
package ucolor
|
||||||
|
|
||||||
import "testing"
|
import "testing"
|
||||||
|
import "image/color"
|
||||||
|
|
||||||
|
func TestTransparent (test *testing.T) {
|
||||||
|
if Transparent(color.NRGBA { A: 255 }) {
|
||||||
|
test.Fatal("false positive")
|
||||||
|
}
|
||||||
|
if !Transparent(color.NRGBA { A: 0 }) {
|
||||||
|
test.Fatal("false negative")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestToRGBA (test *testing.T) {
|
||||||
|
rgba := ToRGBA(color.NRGBA { R: 123, G: 100, B: 23, A: 230 })
|
||||||
|
if rgba != (color.RGBA { R: 111, G: 90, B: 20, A: 230 }) {
|
||||||
|
test.Fatalf("wrong value: %v", rgba)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestPremultiply (test *testing.T) {
|
func TestPremultiply (test *testing.T) {
|
||||||
r, g, b, a := Premultiply(0xFFFF, 0xFFFF, 0xFFFF, 0x8888)
|
r, g, b, a := Premultiply(0xFFFF, 0xFFFF, 0xFFFF, 0x8888)
|
||||||
|
|||||||
94
sync/gate.go
Normal file
94
sync/gate.go
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
package usync
|
||||||
|
|
||||||
|
import "sync"
|
||||||
|
|
||||||
|
// Error defines errors that this package can produce
|
||||||
|
type Error string; const (
|
||||||
|
ErrAlreadyClosed Error = "AlreadyClosed"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Error fullfills the error interface.
|
||||||
|
func (err Error) Error () string {
|
||||||
|
return string(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Gate wraps a channel and allows the receiver to abruptly stop receiving
|
||||||
|
// messages without causing the sender to lock up.
|
||||||
|
type Gate[T any] struct {
|
||||||
|
channel chan T
|
||||||
|
lock sync.RWMutex
|
||||||
|
open bool
|
||||||
|
bufferSize int
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewGate creates a new gate with no buffer.
|
||||||
|
func NewGate[T any] () Gate[T] {
|
||||||
|
return Gate[T] {
|
||||||
|
channel: make(chan T),
|
||||||
|
open: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewBufferedGate creates a new gate with a buffer.
|
||||||
|
func NewBufferedGate[T any] (buffer int) Gate[T] {
|
||||||
|
return Gate[T] {
|
||||||
|
channel: make(chan T, buffer),
|
||||||
|
open: true,
|
||||||
|
bufferSize: buffer,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send sends and item to the channel, returning whether the item was sent.
|
||||||
|
func (this *Gate[T]) Send (item T) bool {
|
||||||
|
this.lock.RLock()
|
||||||
|
defer this.lock.RUnlock()
|
||||||
|
if !this.open { return false }
|
||||||
|
this.channel <- item
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Receive returns a receive-only channel that can be used to receive items.
|
||||||
|
func (this *Gate[T]) Receive () <- chan T {
|
||||||
|
this.lock.RLock()
|
||||||
|
defer this.lock.RUnlock()
|
||||||
|
return this.channel
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close closes the gate, drains all remaining messages, and closes the channel.
|
||||||
|
func (this *Gate[T]) Close () error {
|
||||||
|
this.lock.Lock()
|
||||||
|
if !this.open { return ErrAlreadyClosed }
|
||||||
|
this.open = false
|
||||||
|
this.lock.Unlock()
|
||||||
|
for len(this.channel) > 0 { <- this.channel }
|
||||||
|
close(this.channel)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reset re-opens the gate if it is closed, and creates a new channel.
|
||||||
|
func (this *Gate[T]) Reset () error {
|
||||||
|
this.lock.Lock()
|
||||||
|
defer this.lock.Unlock()
|
||||||
|
this.open = true
|
||||||
|
if this.channel != nil { close(this.channel) }
|
||||||
|
this.channel = make(chan T, this.bufferSize)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Open returns whether the gate is open.
|
||||||
|
func (this *Gate[T]) Open () bool {
|
||||||
|
this.lock.RLock()
|
||||||
|
defer this.lock.RUnlock()
|
||||||
|
return this.open
|
||||||
|
}
|
||||||
|
|
||||||
|
// Len returns the amount of items in the channel.
|
||||||
|
func (this *Gate[T]) Len () int {
|
||||||
|
return len(this.channel)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cap returns the amount of items the channel can hold.
|
||||||
|
func (this *Gate[T]) Cap () int {
|
||||||
|
return cap(this.channel)
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user