]> Sergey Matveev's repositories - btrtrc.git/blob - metainfo/piece.go
Avoid allocating memory when checking interface (#588)
[btrtrc.git] / metainfo / piece.go
1 package metainfo
2
3 import (
4         "github.com/anacrolix/missinggo/v2"
5 )
6
7 type Piece struct {
8         Info *Info // Can we embed the fields here instead, or is it something to do with saving memory?
9         i    pieceIndex
10 }
11
12 type pieceIndex = int
13
14 func (p Piece) Length() int64 {
15         if int(p.i) == p.Info.NumPieces()-1 {
16                 return p.Info.TotalLength() - int64(p.i)*p.Info.PieceLength
17         }
18         return p.Info.PieceLength
19 }
20
21 func (p Piece) Offset() int64 {
22         return int64(p.i) * p.Info.PieceLength
23 }
24
25 func (p Piece) Hash() (ret Hash) {
26         missinggo.CopyExact(&ret, p.Info.Pieces[p.i*HashSize:(p.i+1)*HashSize])
27         return
28 }
29
30 func (p Piece) Index() pieceIndex {
31         return p.i
32 }