Skip to content

Commit e3fcda0

Browse files
committed
Embed noCopy struct into structs, which mustn't be copied
This should help `go vet` detecting invalid structs' copyings. See golang/go#8005 (comment) for details.
1 parent 15e4645 commit e3fcda0

File tree

8 files changed

+30
-0
lines changed

8 files changed

+30
-0
lines changed

args.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ type Args struct {
3939
args []argsKV
4040
bufKV argsKV
4141
buf []byte
42+
43+
noCopy
4244
}
4345

4446
type argsKV struct {

client.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ type Client struct {
201201
mLock sync.Mutex
202202
m map[string]*HostClient
203203
ms map[string]*HostClient
204+
205+
noCopy
204206
}
205207

206208
// Get appends url contents to dst and returns it as body.
@@ -511,6 +513,8 @@ type HostClient struct {
511513

512514
readerPool sync.Pool
513515
writerPool sync.Pool
516+
517+
noCopy
514518
}
515519

516520
type clientConn struct {

cookie.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ type Cookie struct {
5555

5656
bufKV argsKV
5757
buf []byte
58+
59+
noCopy
5860
}
5961

6062
// CopyTo copies src cookie to c.

header.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ type ResponseHeader struct {
3333
bufKV argsKV
3434

3535
cookies []argsKV
36+
37+
noCopy
3638
}
3739

3840
// RequestHeader represents HTTP request header.
@@ -68,6 +70,8 @@ type RequestHeader struct {
6870
cookies []argsKV
6971

7072
rawHeaders []byte
73+
74+
noCopy
7175
}
7276

7377
// SetContentRange sets 'Content-Range: bytes startPos-endPos/contentLength'

http.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ type Request struct {
3737
// Group bool members in order to reduce Request object size.
3838
parsedURI bool
3939
parsedPostArgs bool
40+
41+
noCopy
4042
}
4143

4244
// Response represents HTTP response.
@@ -62,6 +64,8 @@ type Response struct {
6264
// Response.Write() skips writing body if set to true.
6365
// Use it for writing HEAD responses.
6466
SkipBody bool
67+
68+
noCopy
6569
}
6670

6771
// SetRequestURI sets RequestURI.

nocopy.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package fasthttp
2+
3+
// Embed this type into a struct, which mustn't be copied,
4+
// so `go vet` gives a warning if this struct is copied.
5+
//
6+
// See https://github.com/golang/go/issues/8005#issuecomment-190753527 for details.
7+
type noCopy struct {}
8+
func (*noCopy) Lock() {}

server.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,8 @@ type Server struct {
270270
writerPool sync.Pool
271271
hijackConnPool sync.Pool
272272
bytePool sync.Pool
273+
274+
noCopy
273275
}
274276

275277
// TimeoutHandler creates RequestHandler, which returns StatusRequestTimeout
@@ -374,6 +376,8 @@ type RequestCtx struct {
374376
timeoutTimer *time.Timer
375377

376378
hijackHandler HijackHandler
379+
380+
noCopy
377381
}
378382

379383
// HijackHandler must process the hijacked connection c.

uri.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ type URI struct {
5050
requestURI []byte
5151

5252
h *RequestHeader
53+
54+
noCopy
5355
}
5456

5557
// CopyTo copies uri contents to dst.

0 commit comments

Comments
 (0)