1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
package strongunits
// supported units
// B represents bytes
type B uint64
// KiB represents KiB
type KiB uint64
// MiB represents MiB
type MiB uint64
// GiB represents GiB
type GiB uint64
const (
// kibToB is the math convert from bytes to KiB
kibToB = 1 << 10
// mibToB is the math to convert from bytes to MiB
mibToB = 1 << 20
// gibToB s the math to convert from bytes to GiB
gibToB = 1 << 30
)
// StorageUnits is an interface for converting disk/memory storage
// units amongst each other.
type StorageUnits interface {
ToBytes() B
}
// ToBytes is a pass-through function for bytes
func (b B) ToBytes() B {
return b
}
// ToBytes converts KiB to bytes
func (k KiB) ToBytes() B {
return B(k * kibToB)
}
// ToBytes converts MiB to bytes
func (m MiB) ToBytes() B {
return B(m * mibToB)
}
// ToBytes converts GiB to bytes
func (g GiB) ToBytes() B {
return B(g * gibToB)
}
// ToKiB converts any StorageUnit type to KiB
func ToKiB(b StorageUnits) KiB {
return KiB(b.ToBytes() >> 10)
}
// ToMib converts any StorageUnit type to MiB
func ToMib(b StorageUnits) MiB {
return MiB(b.ToBytes() >> 20)
}
// ToGiB converts any StorageUnit type to GiB
func ToGiB(b StorageUnits) GiB {
return GiB(b.ToBytes() >> 30)
}
|