@@ -2,39 +2,41 @@ local bufferWriter = require(script.Parent.Parent.process.bufferWriter)
22local types = require (script .Parent .Parent .types )
33
44local u16 = bufferWriter .u16
5+ local alloc = bufferWriter .alloc
56
67--[[
78 Create a new array with the given dataTypeInterface
89]]
9- return function (valueType : types .dataTypeInterface <any >)
10- local valueWrite = valueType .write
11-
12- return {
13- read = function (b : buffer , cursor : number )
14- local arrayLength = buffer.readu16 (b , cursor )
15- local arrayCursor = cursor + 2
16- local array = {}
17-
18- for _ = 1 , arrayLength do
19- local item , length = valueType .read (b , arrayCursor )
20- table.insert (array , item )
21-
22- arrayCursor += length
23- end
24-
25- return array , (arrayCursor - cursor )
26- end ,
27- write = function (cursor : number , value : any )
28- u16 (cursor , # value ) -- write length, 2 bytes
29- local arrayCursor = cursor + 2
30-
31- for _ , item in value do
32- -- add the length of the item to the cursor
33- arrayCursor += valueWrite (arrayCursor , item )
34- end
35-
36- -- return size of the array: arrayCursor - cursor = size
37- return arrayCursor - cursor
38- end ,
39- }
40- end :: <T >(valueType : types .dataTypeInterface <T >) -> types .dataTypeInterface <{ [number ]: T }>
10+ return function (
11+ valueType : types .dataTypeInterface <
12+ any
13+ >
14+ )
15+ local valueWrite = valueType .write
16+
17+ return {
18+ read = function (b : buffer , cursor : number )
19+ local arrayLength = buffer.readu16 (b , cursor )
20+ local arrayCursor = cursor + 2
21+ local array = {}
22+
23+ for _ = 1 , arrayLength do
24+ local item , length = valueType .read (b , arrayCursor )
25+ table.insert (array , item )
26+
27+ arrayCursor += length
28+ end
29+
30+ return array , (arrayCursor - cursor )
31+ end ,
32+ write = function (value : any )
33+ alloc (2 )
34+ u16 (# value ) -- write length, 2 bytes
35+
36+ for _ , item in value do
37+ -- add the length of the item to the cursor
38+ valueWrite (item )
39+ end
40+ end ,
41+ }
42+ end :: <T >(valueType : types .dataTypeInterface <T >) -> types .dataTypeInterface <{ [number ]: T }>
0 commit comments