修订代码,文档, 示例

This commit is contained in:
e1732a364fed
2022-05-01 09:32:00 +08:00
parent a07a57fdd8
commit efb2ee0010
11 changed files with 75 additions and 41 deletions

View File

@@ -17,6 +17,7 @@ type ByteWriter interface {
Write(p []byte) (n int, err error)
}
//optionally read from OptionalReader
type ReadWrapper struct {
io.Reader
OptionalReader io.Reader
@@ -47,6 +48,7 @@ type DummyReadCloser struct {
ReadCount int
}
// ReadCount -= 1 at each call.
//if ReadCount<0, return 0, io.EOF
func (d *DummyReadCloser) Read(p []byte) (int, error) {
d.ReadCount -= 1
@@ -68,6 +70,7 @@ type DummyWriteCloser struct {
WriteCount int
}
// WriteCount -= 1 at each call.
//if WriteCount<0, return 0, io.EOF
func (d *DummyWriteCloser) Write(p []byte) (int, error) {
d.WriteCount -= 1
@@ -86,10 +89,12 @@ func (DummyWriteCloser) Close() error {
return nil
}
//先从Old读若SwitchChan被关闭, 立刻改为从New读
type ReadSwitcher struct {
Old, New io.Reader
Old, New io.Reader //non-nil
SwitchChan chan struct{} //non-nil
io.Closer
SwitchChan chan struct{}
readOnce sync.Once
@@ -129,7 +134,6 @@ func (d *ReadSwitcher) Read(p []byte) (int, error) {
}
}
//return nil
func (d *ReadSwitcher) Close() error {
if d.Closer != nil {
return d.Closer.Close()
@@ -137,10 +141,11 @@ func (d *ReadSwitcher) Close() error {
return nil
}
//先向Old写若SwitchChan被关闭, 改为向New写
type WriteSwitcher struct {
Old, New io.Writer
Old, New io.Writer //non-nil
SwitchChan chan struct{} //non-nil
io.Closer
SwitchChan chan struct{}
}
func (d *WriteSwitcher) Write(p []byte) (int, error) {
@@ -154,7 +159,6 @@ func (d *WriteSwitcher) Write(p []byte) (int, error) {
}
}
//return nil
func (d *WriteSwitcher) Close() error {
if d.Closer != nil {
return d.Closer.Close()