github.com/elves/elvish@v0.15.0/pkg/eval/vals/pipe.go (about) 1 package vals 2 3 import ( 4 "fmt" 5 "os" 6 7 "github.com/xiaq/persistent/hash" 8 ) 9 10 // Pipe wraps a pair of pointers to os.File that are the two ends of the same 11 // pipe. 12 type Pipe struct { 13 ReadEnd, WriteEnd *os.File 14 } 15 16 var _ interface{} = Pipe{} 17 18 // NewPipe creates a new Pipe value. 19 func NewPipe(r, w *os.File) Pipe { 20 return Pipe{r, w} 21 } 22 23 // Kind returns "pipe". 24 func (Pipe) Kind() string { 25 return "pipe" 26 } 27 28 // Equal compares based on the equality of the two consistuent files. 29 func (p Pipe) Equal(rhs interface{}) bool { 30 q, ok := rhs.(Pipe) 31 if !ok { 32 return false 33 } 34 return Equal(p.ReadEnd, q.ReadEnd) && Equal(p.WriteEnd, q.WriteEnd) 35 } 36 37 // Hash calculates the hash based on the two consituent files. 38 func (p Pipe) Hash() uint32 { 39 return hash.DJB(Hash(p.ReadEnd), Hash(p.WriteEnd)) 40 } 41 42 // Repr writes an opaque representation containing the FDs of the two 43 // constituent files. 44 func (p Pipe) Repr(int) string { 45 return fmt.Sprintf("<pipe{%v %v}>", p.ReadEnd.Fd(), p.WriteEnd.Fd()) 46 }