Slice assignment for openarrays

This commit is contained in:
Mark Spanbroek 2020-09-24 11:44:05 +02:00 committed by markspanbroek
parent 0ca1440bd8
commit eab9253a87
2 changed files with 12 additions and 6 deletions

8
quic/openarray.nim Normal file
View File

@ -0,0 +1,8 @@
proc `[]=`*[T,U,V](
target: var openArray[T],
slice: HSlice[U,V],
replacement: openArray[T]
) =
doAssert replacement.len == slice.len
for i in 0..<replacement.len:
target[slice.a + i] = replacement[i]

View File

@ -2,6 +2,7 @@ import stew/endians2
import datagram
import packet
import ../bits
import ../openarray
proc writeForm*(datagram: var Datagram, packet: Packet) =
datagram[0].bits[0] = Bit(packet.form)
@ -27,8 +28,7 @@ proc writeVersion*(datagram: var Datagram, packet: Packet) =
proc writeConnectionId(datagram: var Datagram, id: ConnectionId, offset: int) =
let bytes = cast[seq[byte]](id)
datagram[offset] = bytes.len.uint8
for i in 0..<bytes.len:
datagram[offset + 1 + i] = bytes[i]
datagram[offset+1..<offset+1+bytes.len] = bytes
proc writeDestination*(datagram: var Datagram, packet: Packet) =
datagram.writeConnectionId(packet.destination, offset=5)
@ -39,11 +39,9 @@ proc writeSource*(datagram: var Datagram, packet: Packet) =
proc writeSupportedVersion*(datagram: var Datagram, packet: Packet) =
let bytes = packet.negotiation.supportedVersion.toBytesBE
let offset = 7 + packet.destination.len + packet.source.len
for i in 0..<bytes.len:
datagram[offset + i] = bytes[i]
datagram[offset..<offset+bytes.len] = bytes
proc writeToken*(datagram: var Datagram, packet: Packet) =
let token = packet.retry.token
let offset = 7 + packet.destination.len + packet.source.len
for i in 0..<token.len:
datagram[offset + i] = token[i]
datagram[offset..<offset+token.len] = token