Skip to content

Commit c147a10

Browse files
Merge pull request #31 from funcool/whimsy
Whimsical improvements (mainly type hints)
2 parents a3f546f + ca185a1 commit c147a10

File tree

8 files changed

+70
-61
lines changed

8 files changed

+70
-61
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ pom.xml.asc
1313
.\#*
1414
/out
1515
/*-init.clj
16-
/doc/dist/
16+
/doc/dist/
17+
.claude/settings.local.json

project.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
:profiles
2525
{:dev {:dependencies [[org.clojure/tools.namespace "0.2.11"]]
2626
:aliases {"test-all" ["with-profile" "dev,1.8:dev,1.7:dev" "test"]}
27-
:global-vars {*warn-on-reflection* false}
27+
:global-vars {*warn-on-reflection* true}
2828
:plugins [[funcool/codeina "0.5.0"]
2929
[lein-ancient "0.6.15"]]}
3030
:1.8 {:dependencies [[org.clojure/clojure "1.8.0"]]}

src/octet/buffer.cljc

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888

8989
#?(:clj
9090
(defn- set-current-bytebuffer-byte-order!
91-
[buff]
91+
[^ByteBuffer buff]
9292
(case *byte-order*
9393
:big-endian (.order buff ByteOrder/BIG_ENDIAN)
9494
:little-endian (.order buff ByteOrder/LITTLE_ENDIAN))))
@@ -137,7 +137,7 @@
137137
(read-ulong [buff pos]
138138
(set-current-bytebuffer-byte-order! buff)
139139
(let [val (.getLong buff pos)
140-
^bytes magnitude (-> (ByteBuffer/allocate 8) (.putLong val) .array)]
140+
^bytes magnitude (-> (ByteBuffer/allocate 8) (.putLong val) (.array))]
141141
(bigint (BigInteger. 1 magnitude))))
142142
(write-ulong [buff pos value]
143143
(set-current-bytebuffer-byte-order! buff)
@@ -163,30 +163,30 @@
163163
IBufferByte
164164
(read-byte [buff pos]
165165
(set-current-bytebuffer-byte-order! buff)
166-
(.get buff pos))
166+
(.get buff ^int pos))
167167
(write-byte [buff pos value]
168168
(set-current-bytebuffer-byte-order! buff)
169-
(.put buff pos value))
169+
(.put buff ^int pos (byte value)))
170170
(read-ubyte [buff pos]
171171
(set-current-bytebuffer-byte-order! buff)
172-
(let [val (.get buff pos)]
172+
(let [val (.get buff ^int pos)]
173173
(bit-and 0xFF (short val))))
174174
(write-ubyte [buff pos value]
175175
(set-current-bytebuffer-byte-order! buff)
176176
(let [value (.byteValue (Short. (short value)))]
177-
(.put buff pos value)))
177+
(.put buff ^int pos value)))
178178

179179
IBufferBytes
180180
(read-bytes [buff pos size]
181181
(let [tmpbuf (byte-array size)
182182
oldpos (.position buff)]
183-
(.position buff pos)
183+
(.position buff ^int pos)
184184
(.get buff tmpbuf)
185185
(.position buff oldpos)
186186
tmpbuf))
187187
(write-bytes [buff pos size data]
188188
(let [oldpos (.position buff)]
189-
(.position buff pos)
189+
(.position buff ^int pos)
190190
(.put buff data 0 size)
191191
(.position buff oldpos)))
192192

@@ -239,7 +239,7 @@
239239
(.setLong buff pos value)))
240240
(read-ulong [buff pos]
241241
(let [val (read-long buff pos)
242-
^bytes magnitude (-> (ByteBuffer/allocate 8) (.putLong val) .array)]
242+
^bytes magnitude (-> (ByteBuffer/allocate 8) (.putLong val) (.array))]
243243
(bigint (BigInteger. 1 magnitude))))
244244
(write-ulong [buff pos value]
245245
(let [value (.longValue (bigint value))]
@@ -280,10 +280,10 @@
280280
IBufferBytes
281281
(read-bytes [buff pos size]
282282
(let [tmpbuf (byte-array size)]
283-
(.getBytes buff pos tmpbuf)
283+
(.getBytes buff ^int pos tmpbuf)
284284
tmpbuf))
285285
(write-bytes [buff pos size data]
286-
(.setBytes buff pos data 0 size))
286+
(.setBytes buff ^int pos ^bytes data 0 ^int size))
287287

288288
IBufferLimit
289289
(get-capacity [buff]
@@ -437,7 +437,7 @@
437437
(let [offset (.-byteOffset buff)
438438
buffer (.-buffer buff)]
439439
(js/Int8Array. buffer (+ offset pos) size)))
440-
(write-bytes [buff pos size data]
440+
(write-bytes [buff pos _size data]
441441
(doseq [i (range (.-length data))]
442442
(.setInt8 buff (+ pos i) (aget data i))))
443443

@@ -471,8 +471,8 @@
471471
This function is defined as multimethod and you can
472472
extend it with your own bytebuffer implementations
473473
if you want or need it."
474-
(fn [size & [{:keys [type impl] :or {type :heap
475-
impl #?(:clj :nio :cljs :es6)}}]]
474+
(fn [_size & [{:keys [type impl] :or {type :heap
475+
impl #?(:clj :nio :cljs :es6)}}]]
476476
[type impl]))
477477

478478
#?(:clj
@@ -488,12 +488,12 @@
488488
#?(:clj
489489
(defmethod allocate [:heap :netty]
490490
[size & _]
491-
(.heapBuffer allocator size)))
491+
(.heapBuffer ^ByteBufAllocator allocator size)))
492492

493493
#?(:clj
494494
(defmethod allocate [:direct :netty]
495495
[size & _]
496-
(.directBuffer allocator size)))
496+
(.directBuffer ^ByteBufAllocator allocator size)))
497497

498498
#?(:cljs
499499
(defmethod allocate [:heap :es6]

src/octet/core.cljc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,16 @@
2424

2525
(ns octet.core
2626
(:refer-clojure :exclude [read byte float double short long bytes into repeat])
27-
(:require [octet.spec :as spec]
28-
#?(:cljs
29-
[octet.util :as util :include-macros true]
30-
:clj
31-
[octet.util :as util])
27+
(:require [octet.buffer :as buffer]
28+
[octet.spec :as spec]
3229
[octet.spec.basic :as basic-spec]
33-
[octet.spec.string :as string-spec]
3430
[octet.spec.collections :as coll-spec]
3531
[octet.spec.reference :as ref-spec]
36-
[octet.buffer :as buffer]))
32+
[octet.spec.string :as string-spec]
33+
#?(:cljs
34+
[octet.util :as util :include-macros true]
35+
:clj
36+
[octet.util :as util])))
3737

3838
(util/defalias compose spec/compose)
3939
(util/defalias spec spec/spec)

src/octet/spec.cljc

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@
3131
3232
For more examples see the `spec` function docstring."
3333
(:refer-clojure :exclude [type read float double long short byte bytes repeat])
34-
(:require [octet.buffer :as buffer]
35-
[octet.util :refer [assoc-ordered]]))
34+
(:require [octet.util :refer [assoc-ordered]]))
3635

3736
;; --- Protocols
3837

@@ -72,10 +71,12 @@
7271
ISpecDynamicSize
7372
(size* [_ data]
7473
(reduce (fn [acc [field data]]
75-
(let [type (field dict)]
74+
(if-let [type (field dict)]
7675
(if (satisfies? ISpecDynamicSize type)
7776
(+ acc (size* type data))
78-
(+ acc (size type)))))
77+
(+ acc (size type)))
78+
; ignore vals that are not in spec
79+
acc))
7980
0
8081
(into [] data)))
8182

@@ -240,7 +241,7 @@
240241

241242
ISpecDynamicSize
242243
(size* [_ data]
243-
(reduce (fn [acc [index data]]
244+
(reduce (fn [acc [_index data]]
244245
(if (satisfies? ISpecSize type)
245246
(+ acc (size type))
246247
(+ acc (size* type data))))

src/octet/spec/string.cljc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
(defn zeropad-count
3434
"Given a byte array, returns a number of bytes
3535
allocated with zero padding (zero byte)."
36-
[input]
36+
[^bytes input]
3737
(let [mark (byte 0)]
3838
(reduce (fn [sum index]
3939
(let [value (aget input index)]
@@ -89,7 +89,7 @@
8989
(js/Int8Array. buff)))
9090

9191
(defn arraycopy
92-
[^bytes input ^bytes output ^long length]
92+
[^bytes input ^bytes output ^long _length]
9393
(reduce (fn [_ i]
9494
(aset output i (aget input i)))
9595
nil

src/octet/util.cljc

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
;; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2424

2525
(ns octet.util
26-
(:require [clojure.string :as str :refer [join]]
27-
[octet.buffer :as bfr])
26+
#?(:clj (:require [clojure.string :as str]
27+
[octet.buffer :as buffer]))
2828

2929
#?(:clj (:import java.util.Arrays)))
3030

@@ -34,9 +34,10 @@
3434
(def ~sym ~sym2)
3535
(alter-meta! (var ~sym) merge (dissoc (meta (var ~sym2)) :name))))
3636

37-
(defn assoc-ordered [a-map key val & rest]
37+
(defn assoc-ordered
3838
"assoc into an array-map, keeping insertion order. The normal clojure
3939
assoc function switches to hash maps on maps > size 10 and loses insertion order"
40+
[a-map key val & rest]
4041
(let [kvs (interleave (concat (keys a-map) (list key))
4142
(concat (vals a-map) (list val)))
4243
ret (apply array-map kvs)]
@@ -55,7 +56,7 @@
5556
fh (fn [_ b]
5657
(let [h (Integer/toHexString (bit-and b 0xFF))]
5758
(if (<= 0 b 15) (str "0" h) h)))]
58-
(join (reductions fh (fh 0 f) r)))))
59+
(str/join (reductions fh (fh 0 f) r)))))
5960

6061
#?(:clj
6162
(defn byte->ascii
@@ -64,57 +65,63 @@
6465
(if (<= 32 (bit-and byte 0xFF) 127) (char byte) \.)))
6566

6667
#?(:clj
67-
(defn- bytes->ascii [^bytes bytes]
68+
(defn- bytes->ascii
6869
"returns a 16-per-line printable ascii view of the bytes"
70+
[^bytes bytes]
6971
(->> bytes
7072
(map byte->ascii)
7173
(partition 16 16 " ")
72-
(map join))))
74+
(map str/join))))
7375

7476
#?(:clj
75-
(defn- format-hex-line [^String hex-line]
77+
(defn- format-hex-line
7678
"formats a 'line' (32 hex chars) of hex output"
79+
[^String hex-line]
7780
(->> hex-line
7881
(partition-all 4)
79-
(map join)
82+
(map str/join)
8083
(split-at 4)
81-
(map #(join " " %))
82-
(join " "))))
84+
(map #(str/join " " %))
85+
(str/join " "))))
8386

8487
#?(:clj
85-
(defn- bytes->hexdump [^bytes bytes]
88+
(defn- bytes->hexdump
8689
"formats a byte array to a sequence of formatted hex lines"
90+
[^bytes bytes]
8791
(->> bytes
88-
bytes->hex
89-
(partition 32 32 (join (repeat 32 " ")))
92+
(bytes->hex)
93+
(partition 32 32 (str/join (repeat 32 " ")))
9094
(map format-hex-line))))
9195

9296
#?(:clj
93-
(defn- copy-bytes [bytes offset size]
97+
(defn- copy-bytes
9498
"utility function - copy bytes, return new byte array"
99+
^bytes
100+
[^bytes bytes ^long offset ^long size]
95101
(let [size (if (nil? size) (alength bytes) size)]
96102
(if (and (= 0 offset) (= (alength bytes) size))
97103
bytes ; short circuit
98-
(java.util.Arrays/copyOfRange bytes
99-
offset
100-
(+ offset size))))))
104+
(Arrays/copyOfRange bytes
105+
offset
106+
^long (+ offset size))))))
101107

102108
#?(:clj
103109
(defn get-dump-bytes
104110
"utility function - return byte array from offset offset and with
105111
size size for nio ByteBuffer, netty ByteBuf, byte array, and String"
112+
^bytes
106113
[x offset size]
107114
(cond
108-
(and (satisfies? octet.buffer/IBufferBytes x)
109-
(satisfies? octet.buffer/IBufferLimit x))
110-
(let [size (if (nil? size) (octet.buffer/get-capacity x) size)]
111-
(octet.buffer/read-bytes x offset size))
115+
(and (satisfies? buffer/IBufferBytes x)
116+
(satisfies? buffer/IBufferLimit x))
117+
(let [size (if (nil? size) (buffer/get-capacity x) size)]
118+
(buffer/read-bytes x offset size))
112119

113120
(instance? (type (byte-array 0)) x)
114121
(copy-bytes x offset size)
115122

116123
(instance? String x)
117-
(copy-bytes (.getBytes x) offset size))))
124+
(copy-bytes (.getBytes ^String x) offset size))))
118125

119126

120127
; Example usage of hex-dump
@@ -156,9 +163,9 @@
156163
ascii (bytes->ascii bytes)
157164
offs (map #(format "%08x" %)
158165
(range offset (+ offset size 16) 16))
159-
header (str " " (join (repeat 68 "-")))
166+
header (str " " (str/join (repeat 68 "-")))
160167
border (if frame "|" "")
161168
lines (map #(str border %1 ": " %2 " " %3 border) offs dump ascii)
162169
lines (if frame (concat [header] lines [header]) lines)
163-
result (join \newline lines)]
170+
result (str/join \newline lines)]
164171
(if print (println result) result))))

test/octet/tests/core.cljc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,25 +72,25 @@
7272
#?(:clj
7373
(t/deftest allocate-heap-nio-buffer
7474
(let [buffer (buf/allocate 16)]
75-
(t/is (not (.isDirect buffer)))
75+
(t/is (not (.isDirect ^ByteBuffer buffer)))
7676
(t/is (instance? ByteBuffer buffer)))))
7777

7878
#?(:clj
7979
(t/deftest allocate-direct-nio-buffer
8080
(let [buffer (buf/allocate 16 {:type :direct})]
81-
(t/is (.isDirect buffer))
81+
(t/is (.isDirect ^ByteBuffer buffer))
8282
(t/is (instance? ByteBuffer buffer)))))
8383

8484
#?(:clj
8585
(t/deftest allocate-heap-netty-buffer
8686
(let [buffer (buf/allocate 16 {:type :heap :impl :netty})]
87-
(t/is (not (.isDirect buffer)))
87+
(t/is (not (.isDirect ^ByteBuf buffer)))
8888
(t/is (instance? ByteBuf buffer)))))
8989

9090
#?(:clj
9191
(t/deftest allocate-direct-netty-buffer
9292
(let [buffer (buf/allocate 16 {:type :direct :impl :netty})]
93-
(t/is (.isDirect buffer))
93+
(t/is (.isDirect ^ByteBuf buffer))
9494
(t/is (instance? ByteBuf buffer)))))
9595

9696
#?(:clj
@@ -99,7 +99,7 @@
9999
buffer (buf/allocate 12)
100100
data [500]]
101101
(t/is (= (buf/write! buffer data spec {:offset 3}) 4))
102-
(t/is (= (.getInt buffer 3) 500)))))
102+
(t/is (= (.getInt ^ByteBuffer buffer 3) 500)))))
103103

104104
#?(:cljs
105105
(t/deftest allocate-direct-es6-buffer

0 commit comments

Comments
 (0)