@@ -584,15 +584,20 @@ public final class Connection {
584
584
/// - block: A collation function that takes two strings and returns the
585
585
/// comparison result.
586
586
public func createCollation( _ collation: String , _ block: @escaping ( _ lhs: String , _ rhs: String ) -> ComparisonResult ) throws {
587
- // TODO correct capacity
588
- let box : Collation = { lhs, rhs in
589
- let lstr = String ( cString: lhs. bindMemory ( to: UInt8 . self, capacity: 0 ) )
590
- let rstr = String ( cString: rhs. bindMemory ( to: UInt8 . self, capacity: 0 ) )
591
- return Int32 ( Int ( block ( lstr, rstr) . rawValue) )
587
+ let box : Collation = { ( lhs: UnsafeRawPointer , rhs: UnsafeRawPointer ) in
588
+ let lstr = String ( cString: lhs. assumingMemoryBound ( to: UInt8 . self) )
589
+ let rstr = String ( cString: rhs. assumingMemoryBound ( to: UInt8 . self) )
590
+ return Int32 ( block ( lstr, rstr) . rawValue)
592
591
}
593
- try check ( sqlite3_create_collation_v2 ( handle, collation, SQLITE_UTF8, unsafeBitCast ( box, to: UnsafeMutableRawPointer . self) , { callback, _, lhs, _, rhs in
594
- unsafeBitCast ( callback, to: Collation . self) ( lhs!, rhs!)
595
- } , nil ) )
592
+ try check ( sqlite3_create_collation_v2 ( handle, collation, SQLITE_UTF8,
593
+ unsafeBitCast ( box, to: UnsafeMutableRawPointer . self) ,
594
+ { ( callback: UnsafeMutableRawPointer ? , _, lhs: UnsafeRawPointer ? , _, rhs: UnsafeRawPointer ? ) in /* xCompare */
595
+ if let lhs = lhs, let rhs = rhs {
596
+ return unsafeBitCast ( callback, to: Collation . self) ( lhs, rhs)
597
+ } else {
598
+ fatalError ( " sqlite3_create_collation_v2 callback called with NULL pointer " )
599
+ }
600
+ } , nil /* xDestroy */) )
596
601
collations [ collation] = box
597
602
}
598
603
fileprivate typealias Collation = @convention ( block) ( UnsafeRawPointer , UnsafeRawPointer ) -> Int32
0 commit comments