/
uzer_007
/
Rixea
Обзор
Документация
Войти
/
uzer_007
/
Rixea
Код
Запросы
0
Задачи
Вики
Пакеты
5
Релизы
2
CI/CD
Аналитика
Безопасность
master
modules/util/sync.go
51 строка
859 B
uzer_007
feat: первая версия Rixea
02 авг 2026, 22:16
02 авг 2026, 22:16
d4f7504
Код
Авторство
О чём код?
// Copyright 2026 Uzer_007. All rights reserved. // SPDX-License-Identifier: MIT package util import ( "sync" "sync/atomic" ) type onceValueResult[T any] struct { value T panic any } // OnceValue is similar to Golang's "sync.OnceValue", but can be reset. type OnceValue[T any] struct { Func func() T mu sync.Mutex res atomic.Pointer[onceValueResult[T]] } func (o *OnceValue[T]) Value() T { res := o.res.Load() if res == nil { o.mu.Lock() defer o.mu.Unlock() res = o.res.Load() if res == nil { res = &onceValueResult[T]{} defer func() { res.panic = recover() o.res.Store(res) if res.panic != nil { panic(res.panic) } }() res.value = o.Func() } } if res.panic != nil { panic(res.panic) } return res.value } func (o *OnceValue[T]) Reset() { o.mu.Lock() defer o.mu.Unlock() o.res.Store(nil) }