Skip to content

Commit 669067d

Browse files
committed
Add and improve a bunch of docstrings
1 parent be2362a commit 669067d

File tree

12 files changed

+196
-42
lines changed

12 files changed

+196
-42
lines changed

counts/counts.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,25 @@ import (
44
"math"
55
)
66

7-
// A count of something, capped at math.MaxUint32.
7+
// Count32 is a count of something, capped at math.MaxUint32.
88
type Count32 uint32
99

10+
// NewCount32 initializes a Count32 from a uint64, capped at
11+
// math.MaxUint32.
1012
func NewCount32(n uint64) Count32 {
1113
if n > math.MaxUint32 {
1214
return Count32(math.MaxUint32)
1315
}
1416
return Count32(n)
1517
}
1618

19+
// ToUint64 returns the value of `n` as a `uint64`. If the value has
20+
// overflowed, it returns `(math.MaxUint32, true)`.
1721
func (n Count32) ToUint64() (uint64, bool) {
1822
return uint64(n), n == math.MaxUint32
1923
}
2024

21-
// Return the sum of two Count32s, capped at math.MaxUint32.
25+
// Plus returns the sum of two Count32s, capped at math.MaxUint32.
2226
func (n1 Count32) Plus(n2 Count32) Count32 {
2327
n := n1 + n2
2428
if n < n1 {
@@ -28,7 +32,7 @@ func (n1 Count32) Plus(n2 Count32) Count32 {
2832
return n
2933
}
3034

31-
// Increment `*n1` by `n2`, capped at math.MaxUint32.
35+
// Increment increases `*n1` by `n2`, capped at math.MaxUint32.
3236
func (n1 *Count32) Increment(n2 Count32) {
3337
*n1 = n1.Plus(n2)
3438
}
@@ -55,18 +59,21 @@ func (n1 *Count32) AdjustMaxIfPossible(n2 Count32) bool {
5559
}
5660
}
5761

58-
// A count of something, capped at math.MaxUint64.
62+
// Count64 is a count of something, capped at math.MaxUint64.
5963
type Count64 uint64
6064

65+
// NewCount64 initializes a Count64 from a uint64.
6166
func NewCount64(n uint64) Count64 {
6267
return Count64(n)
6368
}
6469

70+
// ToUint64 returns the value of `n` as a `uint64`. If the value has
71+
// overflowed, it returns `(math.MaxUint64, true)`.
6572
func (n Count64) ToUint64() (uint64, bool) {
6673
return uint64(n), n == math.MaxUint64
6774
}
6875

69-
// Return the sum of two Count64s, capped at math.MaxUint64.
76+
// Plus returns the sum of two Count64s, capped at math.MaxUint64.
7077
func (n1 Count64) Plus(n2 Count64) Count64 {
7178
n := n1 + n2
7279
if n < n1 {
@@ -76,7 +83,7 @@ func (n1 Count64) Plus(n2 Count64) Count64 {
7683
return n
7784
}
7885

79-
// Increment `*n1` by `n2`, capped at math.MaxUint64.
86+
// Increment increases `*n1` by `n2`, capped at math.MaxUint64.
8087
func (n1 *Count64) Increment(n2 Count64) {
8188
*n1 = n1.Plus(n2)
8289
}

counts/human.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,28 @@ import (
44
"fmt"
55
)
66

7-
// A quantity that can be made human-readable using Human().
7+
// Humanable is a quantity that can be made human-readable using
8+
// `Humaner.Format()`.
89
type Humanable interface {
9-
// Return the value as a uint64, and a boolean telling whether it
10-
// overflowed.
10+
// ToUint64 returns the value as a uint64, and a boolean telling
11+
// whether it overflowed.
1112
ToUint64() (uint64, bool)
1213
}
1314

14-
// An object that can format a Humanable in human-readable format.
15+
// Humaner is an object that can format a Humanable in human-readable
16+
// format.
1517
type Humaner struct {
1618
name string
1719
prefixes []Prefix
1820
}
1921

22+
// Prefix is a metric-like prefix that implies a scaling factor.
2023
type Prefix struct {
2124
Name string
2225
Multiplier uint64
2326
}
2427

28+
// Metric is a Humaner representing metric prefixes.
2529
var Metric = Humaner{
2630
name: "metric",
2731
prefixes: []Prefix{
@@ -34,6 +38,8 @@ var Metric = Humaner{
3438
},
3539
}
3640

41+
// Binary is a Humaner representing power-of-1024 based prefixes,
42+
// typically used for bytes.
3743
var Binary = Humaner{
3844
name: "binary",
3945
prefixes: []Prefix{
@@ -46,12 +52,14 @@ var Binary = Humaner{
4652
},
4753
}
4854

55+
// Name returns the name of `h` ("metric" or "binary").
4956
func (h *Humaner) Name() string {
5057
return h.name
5158
}
5259

53-
// Format n, aligned, in `len(unit) + 10` or fewer characters (except
54-
// for extremely large numbers).
60+
// FormatNumber formats n, aligned, in `len(unit) + 10` or fewer
61+
// characters (except for extremely large numbers). It returns strings
62+
// representing the numeral and the unit string.
5563
func (h *Humaner) FormatNumber(n uint64, unit string) (string, string) {
5664
prefix := h.prefixes[0]
5765

@@ -82,8 +90,9 @@ func (h *Humaner) FormatNumber(n uint64, unit string) (string, string) {
8290
}
8391
}
8492

85-
// Format values, aligned, in `len(unit) + 10` or fewer characters
86-
// (except for extremely large numbers).
93+
// Format formats values, aligned, in `len(unit) + 10` or fewer
94+
// characters (except for extremely large numbers). It returns strings
95+
// representing the numeral and the unit string.
8796
func (h *Humaner) Format(value Humanable, unit string) (string, string) {
8897
n, overflow := value.ToUint64()
8998
if overflow {

0 commit comments

Comments
 (0)