Add force_stdio build flag

This commit is contained in:
世界 2022-09-21 18:16:04 +08:00
parent b2828dac5f
commit 86d7d51023
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
3 changed files with 30 additions and 7 deletions

View file

@ -11,10 +11,10 @@ import (
)
func NewPacketConn(conn net.PacketConn) N.NetPacketConn {
if packetConn, ok := conn.(N.NetPacketConn); ok {
return packetConn
} else if udpConn, ok := conn.(*net.UDPConn); ok {
if udpConn, isUDPConn := conn.(*net.UDPConn); isUDPConn {
return &ExtendedUDPConn{udpConn}
} else if packetConn, isPacketConn := conn.(N.NetPacketConn); isPacketConn && !forceSTDIO {
return packetConn
} else {
return &ExtendedPacketConn{conn}
}
@ -159,8 +159,14 @@ func (r *ExtendedReaderWrapper) ReaderReplaceable() bool {
}
func NewExtendedReader(reader io.Reader) N.ExtendedReader {
if r, ok := reader.(N.ExtendedReader); ok {
return r
if forceSTDIO {
if r, ok := reader.(*ExtendedReaderWrapper); ok {
return r
}
} else {
if r, ok := reader.(N.ExtendedReader); ok {
return r
}
}
return &ExtendedReaderWrapper{reader}
}
@ -187,8 +193,14 @@ func (w *ExtendedReaderWrapper) WriterReplaceable() bool {
}
func NewExtendedWriter(writer io.Writer) N.ExtendedWriter {
if w, ok := writer.(N.ExtendedWriter); ok {
return w
if forceSTDIO {
if w, ok := writer.(*ExtendedWriterWrapper); ok {
return w
}
} else {
if w, ok := writer.(N.ExtendedWriter); ok {
return w
}
}
return &ExtendedWriterWrapper{writer}
}

View file

@ -0,0 +1,6 @@
//go:build force_stdio
package bufio
// force_stdio is dedicated to testing exposed stdio APIs.
const forceSTDIO = true

View file

@ -0,0 +1,5 @@
//go:build !force_stdio
package bufio
const forceSTDIO = false