Skip to content

Commit

Permalink
added compact convertible
Browse files Browse the repository at this point in the history
  • Loading branch information
ypopovych committed Jan 11, 2021
1 parent 9e5b7bf commit e6a7219
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Sources/ScaleCodec/Compact.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import Foundation

private let SCOMPACT_MAX_VALUE = BigUInt(2).power(536) - 1

public protocol CompactConvertible {
var compact: SCompact<BigUInt> { get }
}

public protocol CompactCodable: UnsignedInteger {
static var compactMax: Self { get }
}
Expand All @@ -22,6 +26,14 @@ public struct SCompact<T: CompactCodable>: Equatable, Hashable {
}
}

extension SCompact: CompactConvertible {
public var compact: SCompact<BigUInt> { SCompact<BigUInt>(BigUInt(value)) }
}

extension CompactCodable {
public var compact: SCompact<BigUInt> { SCompact<BigUInt>(BigUInt(self)) }
}

extension SCompact: ScaleEncodable {
public func encode(in encoder: ScaleEncoder) throws {
let u32 = UInt32(clamping: value)
Expand Down Expand Up @@ -133,26 +145,36 @@ extension UInt8: CompactCodable {
}
}

extension UInt8: CompactConvertible {}

extension UInt16: CompactCodable {
public static var compactMax: UInt16 {
return Self.max
}
}

extension UInt16: CompactConvertible {}

extension UInt32: CompactCodable {
public static var compactMax: UInt32 {
return Self.max
}
}

extension UInt32: CompactConvertible {}

extension UInt64: CompactCodable {
public static var compactMax: UInt64 {
return Self.max
}
}

extension UInt64: CompactConvertible {}

extension BigUInt: CompactCodable {
public static var compactMax: BigUInt {
return SCOMPACT_MAX_VALUE
}
}

extension BigUInt: CompactConvertible {}

0 comments on commit e6a7219

Please sign in to comment.