@@ -87,40 +87,52 @@ const (
8787 OpFileSyncTo
8888 // OpFileFlush describes a file flush operation.
8989 OpFileFlush
90+
91+ numOpKinds
9092)
9193
92- // ReadOrWrite returns the operation's kind.
93- func (o OpKind ) ReadOrWrite () OpReadWrite {
94- switch o {
95- case OpOpen , OpOpenDir , OpList , OpStat , OpGetDiskUsage , OpFileRead , OpFileReadAt , OpFileStat :
96- return OpIsRead
97- case OpCreate , OpLink , OpRemove , OpRemoveAll , OpRename , OpReuseForWrite , OpMkdirAll , OpLock , OpFileClose , OpFileWrite , OpFileWriteAt , OpFileSync , OpFileSyncData , OpFileSyncTo , OpFileFlush , OpFilePreallocate :
98- return OpIsWrite
99- default :
100- panic (fmt .Sprintf ("unrecognized op %v\n " , o ))
94+ func (o OpKind ) IsRead () bool {
95+ if o < 0 || o >= numOpKinds {
96+ panic (fmt .Sprintf ("invalid op kind: %d" , o ))
10197 }
98+ return ReadOps .Contains (o )
10299}
103100
104- // OpReadWrite is an enum describing whether an operation is a read or write
105- // operation.
106- type OpReadWrite int
101+ func (o OpKind ) IsWrite () bool {
102+ if o < 0 || o >= numOpKinds {
103+ panic (fmt .Sprintf ("invalid op kind: %d" , o ))
104+ }
105+ return WriteOps .Contains (o )
106+ }
107107
108- const (
109- // OpIsRead describes read operations.
110- OpIsRead OpReadWrite = iota
111- // OpIsWrite describes write operations.
112- OpIsWrite
113- )
108+ // OpKinds represents a set of OpKind values.
109+ type OpKinds uint64
114110
115- // String implements fmt.Stringer.
116- func (kind OpReadWrite ) String () string {
117- switch kind {
118- case OpIsRead :
119- return "Reads"
120- case OpIsWrite :
121- return "Writes"
122- default :
123- panic (fmt .Sprintf ("unrecognized OpKind %d" , kind ))
111+ func MakeOpKinds (kinds ... OpKind ) OpKinds {
112+ var res OpKinds
113+ for _ , kind := range kinds {
114+ res |= OpKinds (1 ) << kind
115+ }
116+ return res
117+ }
118+
119+ func (k OpKinds ) Minus (kind ... OpKind ) OpKinds {
120+ return k &^ MakeOpKinds (kind ... )
121+ }
122+
123+ func (k OpKinds ) Contains (kind OpKind ) bool {
124+ return k & (OpKinds (1 )<< kind ) != 0
125+ }
126+
127+ var ReadOps = MakeOpKinds (OpOpen , OpOpenDir , OpList , OpStat , OpGetDiskUsage , OpFileRead , OpFileReadAt , OpFileStat )
128+ var WriteOps = MakeOpKinds (OpCreate , OpLink , OpRemove , OpRemoveAll , OpRename , OpReuseForWrite , OpMkdirAll , OpLock , OpFileClose , OpFileWrite , OpFileWriteAt , OpFileSync , OpFileSyncData , OpFileSyncTo , OpFileFlush , OpFilePreallocate )
129+
130+ func init () {
131+ if ReadOps & WriteOps != 0 {
132+ panic ("some op is both read and write" )
133+ }
134+ if ReadOps | WriteOps != (OpKinds (1 )<< numOpKinds - 1 ) {
135+ panic ("some op is neither read nor write" )
124136 }
125137}
126138
0 commit comments