mirror of
				https://github.com/superseriousbusiness/gotosocial.git
				synced 2025-11-03 23:02:24 -06:00 
			
		
		
		
	
		
			
				
	
	
		
			43 lines
		
	
	
	
		
			795 B
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			43 lines
		
	
	
	
		
			795 B
		
	
	
	
		
			Go
		
	
	
	
	
	
package iotools
 | 
						|
 | 
						|
import "io"
 | 
						|
 | 
						|
// CloserFunc is a function signature which allows
 | 
						|
// a function to implement the io.Closer type.
 | 
						|
type CloserFunc func() error
 | 
						|
 | 
						|
func (c CloserFunc) Close() error {
 | 
						|
	return c()
 | 
						|
}
 | 
						|
 | 
						|
func CloserCallback(c io.Closer, cb func()) io.Closer {
 | 
						|
	return CloserFunc(func() error {
 | 
						|
		defer cb()
 | 
						|
		return c.Close()
 | 
						|
	})
 | 
						|
}
 | 
						|
 | 
						|
func CloserAfterCallback(c io.Closer, cb func()) io.Closer {
 | 
						|
	return CloserFunc(func() (err error) {
 | 
						|
		defer func() { err = c.Close() }()
 | 
						|
		cb()
 | 
						|
		return
 | 
						|
	})
 | 
						|
}
 | 
						|
 | 
						|
// CloseOnce wraps an io.Closer to ensure it only performs the close logic once.
 | 
						|
func CloseOnce(c io.Closer) io.Closer {
 | 
						|
	return CloserFunc(func() error {
 | 
						|
		if c == nil {
 | 
						|
			// already run.
 | 
						|
			return nil
 | 
						|
		}
 | 
						|
 | 
						|
		// Acquire.
 | 
						|
		cptr := c
 | 
						|
		c = nil
 | 
						|
 | 
						|
		// Call the closer.
 | 
						|
		return cptr.Close()
 | 
						|
	})
 | 
						|
}
 |