// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package sync

// OnceFunc returns a function that invokes f only once. The returned function
// may be called concurrently.
//
// If f panics, the returned function will panic with the same value on every call.
func ( func()) func() {
	var (
		  Once
		 bool
		     any
	)
	// Construct the inner closure just once to reduce costs on the fast path.
	 := func() {
		defer func() {
			 = recover()
			if ! {
				// Re-panic immediately so on the first call the user gets a
				// complete stack trace into f.
				panic()
			}
		}()
		()
		 = true // Set only if f does not panic
	}
	return func() {
		.Do()
		if ! {
			panic()
		}
	}
}

// OnceValue returns a function that invokes f only once and returns the value
// returned by f. The returned function may be called concurrently.
//
// If f panics, the returned function will panic with the same value on every call.
func [ any]( func() ) func()  {
	var (
		   Once
		  bool
		      any
		 
	)
	 := func() {
		defer func() {
			 = recover()
			if ! {
				panic()
			}
		}()
		 = ()
		 = true
	}
	return func()  {
		.Do()
		if ! {
			panic()
		}
		return 
	}
}

// OnceValues returns a function that invokes f only once and returns the values
// returned by f. The returned function may be called concurrently.
//
// If f panics, the returned function will panic with the same value on every call.
func [,  any]( func() (, )) func() (, ) {
	var (
		  Once
		 bool
		     any
		    
		    
	)
	 := func() {
		defer func() {
			 = recover()
			if ! {
				panic()
			}
		}()
		,  = ()
		 = true
	}
	return func() (, ) {
		.Do()
		if ! {
			panic()
		}
		return , 
	}
}