Skip to content

Commit b10919f

Browse files
bitwarden/sdk-internal@c975847 1.0.0-2487-c975847 - [PM-23785] Use actual error types in UniFFI (bitwarden/sdk-internal#424)
1 parent 0dae8bf commit b10919f

14 files changed

+7148
-1646
lines changed

Package.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ let package = Package(
3030
dependencies: ["BitwardenSdk"]),
3131
.binaryTarget(
3232
name: "BitwardenFFI",
33-
url: "https://github.com/bitwarden/sdk-swift/releases/download/v1.0.0-2483-d0262d3/BitwardenFFI-1.0.0-d0262d3.xcframework.zip",
34-
checksum: "795070076d259c537451f25f997d019d040bbdd8656f57153fe4b0106543b8f4")
33+
url: "https://github.com/bitwarden/sdk-swift/releases/download/v1.0.0-2487-c975847/BitwardenFFI-1.0.0-c975847.xcframework.zip",
34+
checksum: "f6b37a81b6b040b045c364197a616af2bf691a6d9053cac11a88d93603514827")
3535
]
3636
)

Sources/BitwardenSdk/BitwardenCollections.swift

Lines changed: 116 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -352,21 +352,31 @@ private func uniffiTraitInterfaceCallWithError<T, E>(
352352
callStatus.pointee.errorBuf = FfiConverterString.lower(String(describing: error))
353353
}
354354
}
355+
// Initial value and increment amount for handles.
356+
// These ensure that SWIFT handles always have the lowest bit set
357+
fileprivate let UNIFFI_HANDLEMAP_INITIAL: UInt64 = 1
358+
fileprivate let UNIFFI_HANDLEMAP_DELTA: UInt64 = 2
359+
355360
fileprivate final class UniffiHandleMap<T>: @unchecked Sendable {
356361
// All mutation happens with this lock held, which is why we implement @unchecked Sendable.
357362
private let lock = NSLock()
358363
private var map: [UInt64: T] = [:]
359-
private var currentHandle: UInt64 = 1
364+
private var currentHandle: UInt64 = UNIFFI_HANDLEMAP_INITIAL
360365

361366
func insert(obj: T) -> UInt64 {
362367
lock.withLock {
363-
let handle = currentHandle
364-
currentHandle += 1
365-
map[handle] = obj
366-
return handle
368+
return doInsert(obj)
367369
}
368370
}
369371

372+
// Low-level insert function, this assumes `lock` is held.
373+
private func doInsert(_ obj: T) -> UInt64 {
374+
let handle = currentHandle
375+
currentHandle += UNIFFI_HANDLEMAP_DELTA
376+
map[handle] = obj
377+
return handle
378+
}
379+
370380
func get(handle: UInt64) throws -> T {
371381
try lock.withLock {
372382
guard let obj = map[handle] else {
@@ -376,6 +386,15 @@ fileprivate final class UniffiHandleMap<T>: @unchecked Sendable {
376386
}
377387
}
378388

389+
func clone(handle: UInt64) throws -> UInt64 {
390+
try lock.withLock {
391+
guard let obj = map[handle] else {
392+
throw UniffiInternalError.unexpectedStaleHandle
393+
}
394+
return doInsert(obj)
395+
}
396+
}
397+
379398
@discardableResult
380399
func remove(handle: UInt64) throws -> T {
381400
try lock.withLock {
@@ -494,6 +513,9 @@ extension Collection: Sendable {}
494513
#endif
495514

496515

516+
517+
518+
497519
extension Collection: Equatable, Hashable {
498520
public static func ==(lhs: Collection, rhs: Collection) -> Bool {
499521
if lhs.id != rhs.id {
@@ -618,6 +640,9 @@ extension CollectionView: Sendable {}
618640
#endif
619641

620642

643+
644+
645+
621646
extension CollectionView: Equatable, Hashable {
622647
public static func ==(lhs: CollectionView, rhs: CollectionView) -> Bool {
623648
if lhs.id != rhs.id {
@@ -706,6 +731,82 @@ public func FfiConverterTypeCollectionView_lower(_ value: CollectionView) -> Rus
706731
return FfiConverterTypeCollectionView.lower(value)
707732
}
708733

734+
735+
public enum CollectionDecryptError: Swift.Error {
736+
737+
738+
739+
case Crypto(message: String)
740+
741+
}
742+
743+
744+
#if swift(>=5.8)
745+
@_documentation(visibility: private)
746+
#endif
747+
public struct FfiConverterTypeCollectionDecryptError: FfiConverterRustBuffer {
748+
typealias SwiftType = CollectionDecryptError
749+
750+
public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> CollectionDecryptError {
751+
let variant: Int32 = try readInt(&buf)
752+
switch variant {
753+
754+
755+
756+
757+
case 1: return .Crypto(
758+
message: try FfiConverterString.read(from: &buf)
759+
)
760+
761+
762+
default: throw UniffiInternalError.unexpectedEnumCase
763+
}
764+
}
765+
766+
public static func write(_ value: CollectionDecryptError, into buf: inout [UInt8]) {
767+
switch value {
768+
769+
770+
771+
772+
case .Crypto(_ /* message is ignored*/):
773+
writeInt(&buf, Int32(1))
774+
775+
776+
}
777+
}
778+
}
779+
780+
781+
#if swift(>=5.8)
782+
@_documentation(visibility: private)
783+
#endif
784+
public func FfiConverterTypeCollectionDecryptError_lift(_ buf: RustBuffer) throws -> CollectionDecryptError {
785+
return try FfiConverterTypeCollectionDecryptError.lift(buf)
786+
}
787+
788+
#if swift(>=5.8)
789+
@_documentation(visibility: private)
790+
#endif
791+
public func FfiConverterTypeCollectionDecryptError_lower(_ value: CollectionDecryptError) -> RustBuffer {
792+
return FfiConverterTypeCollectionDecryptError.lower(value)
793+
}
794+
795+
796+
extension CollectionDecryptError: Equatable, Hashable {}
797+
798+
799+
800+
801+
extension CollectionDecryptError: Foundation.LocalizedError {
802+
public var errorDescription: String? {
803+
String(reflecting: self)
804+
}
805+
}
806+
807+
808+
809+
709810
// Note that we don't yet support `indirect` for enums.
710811
// See https://github.com/mozilla/uniffi-rs/issues/396 for further discussion.
711812
/**
@@ -723,9 +824,8 @@ public enum CollectionType : UInt8 {
723824
* OrganizationDataOwnership (formerly PersonalOwnership) policy enabled.
724825
*/
725826
case defaultUserCollection = 1
726-
}
727-
728827

828+
}
729829
#if compiler(>=6)
730830
extension CollectionType: Sendable {}
731831
#endif
@@ -779,10 +879,17 @@ public func FfiConverterTypeCollectionType_lower(_ value: CollectionType) -> Rus
779879
}
780880

781881

882+
883+
782884
extension CollectionType: Equatable, Hashable {}
783885

784886

785887

888+
889+
890+
891+
892+
786893
#if swift(>=5.8)
787894
@_documentation(visibility: private)
788895
#endif
@@ -884,15 +991,15 @@ private enum InitializationResult {
884991
// the code inside is only computed once.
885992
private let initializationResult: InitializationResult = {
886993
// Get the bindings contract version from our ComponentInterface
887-
let bindings_contract_version = 29
994+
let bindings_contract_version = 30
888995
// Get the scaffolding contract version by calling the into the dylib
889996
let scaffolding_contract_version = ffi_bitwarden_collections_uniffi_contract_version()
890997
if bindings_contract_version != scaffolding_contract_version {
891998
return InitializationResult.contractVersionMismatch
892999
}
8931000

894-
uniffiEnsureBitwardenCoreInitialized()
8951001
uniffiEnsureBitwardenCryptoInitialized()
1002+
uniffiEnsureBitwardenCoreInitialized()
8961003
return InitializationResult.ok
8971004
}()
8981005

0 commit comments

Comments
 (0)