Changed init(Data) to init<D: DataProtocol>(D)

This commit is contained in:
Adam Fowler 2020-02-14 09:50:48 +00:00
parent b9b08ccd16
commit 133332918e
1 changed files with 8 additions and 4 deletions

View File

@ -44,11 +44,15 @@ public final class BigNum {
self.ctx = ctx!.convert()
}
public init(data: Data) {
public init<D: DataProtocol>(data: D) {
let ctx = BN_new()
_ = data.withUnsafeBytes { bytes in
let p = bytes.bindMemory(to: UInt8.self)
BN_bin2bn(p.baseAddress, Int32(data.count), ctx)
if data.withContiguousStorageIfAvailable({bytes in
BN_bin2bn(bytes.baseAddress, .init(data.count), ctx)
}) == nil {
var buffer = UnsafeMutableBufferPointer<UInt8>.allocate(capacity: data.count)
data.copyBytes(to: buffer)
defer { buffer.deallocate() }
BN_bin2bn(buffer.baseAddress, .init(data.count), ctx)
}
self.ctx = ctx!.convert()
}