FromProto sets BitArray to the given protoBitArray. It returns an error if protoBitArray is invalid.
(protoBitArray *tmprotobits.BitArray)
| 441 | // FromProto sets BitArray to the given protoBitArray. It returns an error if |
| 442 | // protoBitArray is invalid. |
| 443 | func (bA *BitArray) FromProto(protoBitArray *tmprotobits.BitArray) error { |
| 444 | if protoBitArray == nil { |
| 445 | return nil |
| 446 | } |
| 447 | |
| 448 | // Validate protoBitArray. |
| 449 | if protoBitArray.Bits < 0 { |
| 450 | return errors.New("negative Bits") |
| 451 | } |
| 452 | // #[32bit] |
| 453 | if protoBitArray.Bits > math.MaxInt32 { // prevent overflow on 32bit systems |
| 454 | return errors.New("too many Bits") |
| 455 | } |
| 456 | if got, exp := len(protoBitArray.Elems), numElems(int(protoBitArray.Bits)); got != exp { |
| 457 | return fmt.Errorf("invalid number of Elems: got %d, but exp %d", got, exp) |
| 458 | } |
| 459 | |
| 460 | bA.mtx.Lock() |
| 461 | defer bA.mtx.Unlock() |
| 462 | |
| 463 | ec := make([]uint64, len(protoBitArray.Elems)) |
| 464 | copy(ec, protoBitArray.Elems) |
| 465 | |
| 466 | bA.Bits = int(protoBitArray.Bits) |
| 467 | bA.Elems = ec |
| 468 | return nil |
| 469 | } |
| 470 | |
| 471 | func numElems(bits int) int { |
| 472 | return (bits + 63) / 64 |