]> Sergey Matveev's repositories - btrtrc.git/blobdiff - misc.go
Drop support for go 1.20
[btrtrc.git] / misc.go
diff --git a/misc.go b/misc.go
index 508f0a63441ca090547670598296b202a6fd10e2..7d3007ecee74eb92f58eee434dfb9431a86f9098 100644 (file)
--- a/misc.go
+++ b/misc.go
@@ -4,12 +4,14 @@ import (
        "errors"
        "net"
 
+       "github.com/RoaringBitmap/roaring"
        "github.com/anacrolix/missinggo/v2"
-       "github.com/anacrolix/torrent/types"
        "golang.org/x/time/rate"
 
        "github.com/anacrolix/torrent/metainfo"
        pp "github.com/anacrolix/torrent/peer_protocol"
+       "github.com/anacrolix/torrent/types"
+       "github.com/anacrolix/torrent/types/infohash"
 )
 
 type (
@@ -43,7 +45,7 @@ func newRequestFromMessage(msg *pp.Message) Request {
 }
 
 // The size in bytes of a metadata extension piece.
-func metadataPieceSize(totalSize int, piece int) int {
+func metadataPieceSize(totalSize, piece int) int {
        ret := totalSize - piece*(1<<14)
        if ret > 1<<14 {
                ret = 1 << 14
@@ -52,8 +54,11 @@ func metadataPieceSize(totalSize int, piece int) int {
 }
 
 // Return the request that would include the given offset into the torrent data.
-func torrentOffsetRequest(torrentLength, pieceSize, chunkSize, offset int64) (
-       r Request, ok bool) {
+func torrentOffsetRequest(
+       torrentLength, pieceSize, chunkSize, offset int64,
+) (
+       r Request, ok bool,
+) {
        if offset < 0 || offset >= torrentLength {
                return
        }
@@ -96,7 +101,7 @@ func validateInfo(info *metainfo.Info) error {
        return nil
 }
 
-func chunkIndexSpec(index pp.Integer, pieceLength, chunkSize pp.Integer) ChunkSpec {
+func chunkIndexSpec(index, pieceLength, chunkSize pp.Integer) ChunkSpec {
        ret := ChunkSpec{pp.Integer(index) * chunkSize, chunkSize}
        if ret.Begin+ret.Length > pieceLength {
                ret.Length = pieceLength - ret.Begin
@@ -110,7 +115,8 @@ func connLessTrusted(l, r *Peer) bool {
 
 func connIsIpv6(nc interface {
        LocalAddr() net.Addr
-}) bool {
+},
+) bool {
        ra := nc.LocalAddr()
        rip := addrIpOrNil(ra)
        return rip.To4() == nil && rip.To16() != nil
@@ -139,6 +145,16 @@ func max(as ...int64) int64 {
        return ret
 }
 
+func maxInt(as ...int) int {
+       ret := as[0]
+       for _, a := range as[1:] {
+               if a > ret {
+                       ret = a
+               }
+       }
+       return ret
+}
+
 func min(as ...int64) int64 {
        ret := as[0]
        for _, a := range as[1:] {
@@ -163,6 +179,16 @@ var unlimited = rate.NewLimiter(rate.Inf, 0)
 
 type (
        pieceIndex = int
-       InfoHash   = metainfo.Hash
-       IpPort     = missinggo.IpPort
+       // Deprecated: Use infohash.T directly to avoid unnecessary imports.
+       InfoHash = infohash.T
+       IpPort   = missinggo.IpPort
 )
+
+func boolSliceToBitmap(slice []bool) (rb roaring.Bitmap) {
+       for i, b := range slice {
+               if b {
+                       rb.AddInt(i)
+               }
+       }
+       return
+}