From 93a8ea1d31cb51f82d5c3912312826eb746da9a4 Mon Sep 17 00:00:00 2001 From: MacOMNI <414294494@qq.com> Date: Wed, 21 Aug 2024 15:00:12 +0800 Subject: [PATCH] update bitstring --- Utils/Sources/Utils/FixSizeBitstring.swift | 24 ++++++++++++++-------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/Utils/Sources/Utils/FixSizeBitstring.swift b/Utils/Sources/Utils/FixSizeBitstring.swift index 367cf705..21fd27c6 100644 --- a/Utils/Sources/Utils/FixSizeBitstring.swift +++ b/Utils/Sources/Utils/FixSizeBitstring.swift @@ -5,12 +5,12 @@ import ScaleCodec public struct FixSizeBitstring: Hashable, Sendable { /// Byte storage for bits. public private(set) var bytes: Data + /// length of the bitstring public private(set) var length: Int - /// Initialize with byte data storage. + /// Initialize with byte data storage and length. /// - Parameter bytes: Byte storage for bits. - public init?(config: T.TConfig, bytes: Data) { - let length = T.read(config: config) - guard length == bytes.count else { + public init?(bytes: Data, length: Int) { + guard bytes.count * 8 >= length else { return nil } self.bytes = bytes @@ -55,14 +55,20 @@ extension FixSizeBitstring: Equatable { - returns true if the bitstrings are equal, false otherwise */ public static func == (lhs: FixSizeBitstring, rhs: FixSizeBitstring) -> Bool { - return lhs.length == rhs.length && lhs.bytes == rhs.bytes + lhs.length == rhs.length && lhs.bytes == rhs.bytes } } +extension FixSizeBitstring: ScaleCodec.Encodable { + public init(config: TByteLength.TConfig, from decoder: inout some ScaleCodec.Decoder) throws { + let length = TByteLength.read(config: config) + try self.init( + bytes: decoder.decode(Data.self), + length: length + )! + } -extension FixSizeBitstring where T: ScaleCodec.Decodable { - public init(config: T.TConfig, from decoder: inout some ScaleCodec.Decoder) throws { - let length = T.read(config: config) - try self.init(bytes: decoder.decode(Data.self, .fixed(UInt(length))), length: length) + public func encode(in encoder: inout some ScaleCodec.Encoder) throws { + try encoder.encode(bytes) } }