github.com/markusbkk/elvish@v0.0.0-20231204143114-91dc52438621/pkg/eval/vals/pipe.go (about)

     1  package vals
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"github.com/markusbkk/elvish/pkg/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 _ PseudoStructMap = 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 constituent 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  }
    47  
    48  // Fields returns fields of the Pipe value.
    49  func (p Pipe) Fields() StructMap {
    50  	return pipeFields{p.ReadEnd, p.WriteEnd}
    51  }
    52  
    53  type pipeFields struct{ R, W *os.File }
    54  
    55  func (pipeFields) IsStructMap() {}