Revision control

Copy as Markdown

// This file was autogenerated by some hot garbage in the `uniffi` crate.
// Trust me, you don't want to mess with it!
// swiftlint:disable all
import Foundation
// Depending on the consumer's build setup, the low-level FFI code
// might be in a separate module, or it might be compiled inline into
// this module. This is a bit of light hackery to work with both.
#if canImport(MozillaRustComponents)
import MozillaRustComponents
#endif
fileprivate extension RustBuffer {
// Allocate a new buffer, copying the contents of a `UInt8` array.
init(bytes: [UInt8]) {
let rbuf = bytes.withUnsafeBufferPointer { ptr in
RustBuffer.from(ptr)
}
self.init(capacity: rbuf.capacity, len: rbuf.len, data: rbuf.data)
}
static func empty() -> RustBuffer {
RustBuffer(capacity: 0, len:0, data: nil)
}
static func from(_ ptr: UnsafeBufferPointer<UInt8>) -> RustBuffer {
try! rustCall { ffi_viaduct_rustbuffer_from_bytes(ForeignBytes(bufferPointer: ptr), $0) }
}
// Frees the buffer in place.
// The buffer must not be used after this is called.
func deallocate() {
try! rustCall { ffi_viaduct_rustbuffer_free(self, $0) }
}
}
fileprivate extension ForeignBytes {
init(bufferPointer: UnsafeBufferPointer<UInt8>) {
self.init(len: Int32(bufferPointer.count), data: bufferPointer.baseAddress)
}
}
// For every type used in the interface, we provide helper methods for conveniently
// lifting and lowering that type from C-compatible data, and for reading and writing
// values of that type in a buffer.
// Helper classes/extensions that don't change.
// Someday, this will be in a library of its own.
fileprivate extension Data {
init(rustBuffer: RustBuffer) {
self.init(
bytesNoCopy: rustBuffer.data!,
count: Int(rustBuffer.len),
deallocator: .none
)
}
}
// Define reader functionality. Normally this would be defined in a class or
// struct, but we use standalone functions instead in order to make external
// types work.
//
// With external types, one swift source file needs to be able to call the read
// method on another source file's FfiConverter, but then what visibility
// should Reader have?
// - If Reader is fileprivate, then this means the read() must also
// be fileprivate, which doesn't work with external types.
// - If Reader is internal/public, we'll get compile errors since both source
// files will try define the same type.
//
// Instead, the read() method and these helper functions input a tuple of data
fileprivate func createReader(data: Data) -> (data: Data, offset: Data.Index) {
(data: data, offset: 0)
}
// Reads an integer at the current offset, in big-endian order, and advances
// the offset on success. Throws if reading the integer would move the
// offset past the end of the buffer.
fileprivate func readInt<T: FixedWidthInteger>(_ reader: inout (data: Data, offset: Data.Index)) throws -> T {
let range = reader.offset..<reader.offset + MemoryLayout<T>.size
guard reader.data.count >= range.upperBound else {
throw UniffiInternalError.bufferOverflow
}
if T.self == UInt8.self {
let value = reader.data[reader.offset]
reader.offset += 1
return value as! T
}
var value: T = 0
let _ = withUnsafeMutableBytes(of: &value, { reader.data.copyBytes(to: $0, from: range)})
reader.offset = range.upperBound
return value.bigEndian
}
// Reads an arbitrary number of bytes, to be used to read
// raw bytes, this is useful when lifting strings
fileprivate func readBytes(_ reader: inout (data: Data, offset: Data.Index), count: Int) throws -> Array<UInt8> {
let range = reader.offset..<(reader.offset+count)
guard reader.data.count >= range.upperBound else {
throw UniffiInternalError.bufferOverflow
}
var value = [UInt8](repeating: 0, count: count)
value.withUnsafeMutableBufferPointer({ buffer in
reader.data.copyBytes(to: buffer, from: range)
})
reader.offset = range.upperBound
return value
}
// Reads a float at the current offset.
fileprivate func readFloat(_ reader: inout (data: Data, offset: Data.Index)) throws -> Float {
return Float(bitPattern: try readInt(&reader))
}
// Reads a float at the current offset.
fileprivate func readDouble(_ reader: inout (data: Data, offset: Data.Index)) throws -> Double {
return Double(bitPattern: try readInt(&reader))
}
// Indicates if the offset has reached the end of the buffer.
fileprivate func hasRemaining(_ reader: (data: Data, offset: Data.Index)) -> Bool {
return reader.offset < reader.data.count
}
// Define writer functionality. Normally this would be defined in a class or
// struct, but we use standalone functions instead in order to make external
// types work. See the above discussion on Readers for details.
fileprivate func createWriter() -> [UInt8] {
return []
}
fileprivate func writeBytes<S>(_ writer: inout [UInt8], _ byteArr: S) where S: Sequence, S.Element == UInt8 {
writer.append(contentsOf: byteArr)
}
// Writes an integer in big-endian order.
//
// Warning: make sure what you are trying to write
// is in the correct type!
fileprivate func writeInt<T: FixedWidthInteger>(_ writer: inout [UInt8], _ value: T) {
var value = value.bigEndian
withUnsafeBytes(of: &value) { writer.append(contentsOf: $0) }
}
fileprivate func writeFloat(_ writer: inout [UInt8], _ value: Float) {
writeInt(&writer, value.bitPattern)
}
fileprivate func writeDouble(_ writer: inout [UInt8], _ value: Double) {
writeInt(&writer, value.bitPattern)
}
// Protocol for types that transfer other types across the FFI. This is
// analogous to the Rust trait of the same name.
fileprivate protocol FfiConverter {
associatedtype FfiType
associatedtype SwiftType
static func lift(_ value: FfiType) throws -> SwiftType
static func lower(_ value: SwiftType) -> FfiType
static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> SwiftType
static func write(_ value: SwiftType, into buf: inout [UInt8])
}
// Types conforming to `Primitive` pass themselves directly over the FFI.
fileprivate protocol FfiConverterPrimitive: FfiConverter where FfiType == SwiftType { }
extension FfiConverterPrimitive {
#if swift(>=5.8)
@_documentation(visibility: private)
#endif
public static func lift(_ value: FfiType) throws -> SwiftType {
return value
}
#if swift(>=5.8)
@_documentation(visibility: private)
#endif
public static func lower(_ value: SwiftType) -> FfiType {
return value
}
}
// Types conforming to `FfiConverterRustBuffer` lift and lower into a `RustBuffer`.
// Used for complex types where it's hard to write a custom lift/lower.
fileprivate protocol FfiConverterRustBuffer: FfiConverter where FfiType == RustBuffer {}
extension FfiConverterRustBuffer {
#if swift(>=5.8)
@_documentation(visibility: private)
#endif
public static func lift(_ buf: RustBuffer) throws -> SwiftType {
var reader = createReader(data: Data(rustBuffer: buf))
let value = try read(from: &reader)
if hasRemaining(reader) {
throw UniffiInternalError.incompleteData
}
buf.deallocate()
return value
}
#if swift(>=5.8)
@_documentation(visibility: private)
#endif
public static func lower(_ value: SwiftType) -> RustBuffer {
var writer = createWriter()
write(value, into: &writer)
return RustBuffer(bytes: writer)
}
}
// An error type for FFI errors. These errors occur at the UniFFI level, not
// the library level.
fileprivate enum UniffiInternalError: LocalizedError {
case bufferOverflow
case incompleteData
case unexpectedOptionalTag
case unexpectedEnumCase
case unexpectedNullPointer
case unexpectedRustCallStatusCode
case unexpectedRustCallError
case unexpectedStaleHandle
case rustPanic(_ message: String)
public var errorDescription: String? {
switch self {
case .bufferOverflow: return "Reading the requested value would read past the end of the buffer"
case .incompleteData: return "The buffer still has data after lifting its containing value"
case .unexpectedOptionalTag: return "Unexpected optional tag; should be 0 or 1"
case .unexpectedEnumCase: return "Raw enum value doesn't match any cases"
case .unexpectedNullPointer: return "Raw pointer value was null"
case .unexpectedRustCallStatusCode: return "Unexpected RustCallStatus code"
case .unexpectedRustCallError: return "CALL_ERROR but no errorClass specified"
case .unexpectedStaleHandle: return "The object in the handle map has been dropped already"
case let .rustPanic(message): return message
}
}
}
fileprivate extension NSLock {
func withLock<T>(f: () throws -> T) rethrows -> T {
self.lock()
defer { self.unlock() }
return try f()
}
}
fileprivate let CALL_SUCCESS: Int8 = 0
fileprivate let CALL_ERROR: Int8 = 1
fileprivate let CALL_UNEXPECTED_ERROR: Int8 = 2
fileprivate let CALL_CANCELLED: Int8 = 3
fileprivate extension RustCallStatus {
init() {
self.init(
code: CALL_SUCCESS,
errorBuf: RustBuffer.init(
capacity: 0,
len: 0,
data: nil
)
)
}
}
private func rustCall<T>(_ callback: (UnsafeMutablePointer<RustCallStatus>) -> T) throws -> T {
let neverThrow: ((RustBuffer) throws -> Never)? = nil
return try makeRustCall(callback, errorHandler: neverThrow)
}
private func rustCallWithError<T, E: Swift.Error>(
_ errorHandler: @escaping (RustBuffer) throws -> E,
_ callback: (UnsafeMutablePointer<RustCallStatus>) -> T) throws -> T {
try makeRustCall(callback, errorHandler: errorHandler)
}
private func makeRustCall<T, E: Swift.Error>(
_ callback: (UnsafeMutablePointer<RustCallStatus>) -> T,
errorHandler: ((RustBuffer) throws -> E)?
) throws -> T {
uniffiEnsureViaductInitialized()
var callStatus = RustCallStatus.init()
let returnedVal = callback(&callStatus)
try uniffiCheckCallStatus(callStatus: callStatus, errorHandler: errorHandler)
return returnedVal
}
private func uniffiCheckCallStatus<E: Swift.Error>(
callStatus: RustCallStatus,
errorHandler: ((RustBuffer) throws -> E)?
) throws {
switch callStatus.code {
case CALL_SUCCESS:
return
case CALL_ERROR:
if let errorHandler = errorHandler {
throw try errorHandler(callStatus.errorBuf)
} else {
callStatus.errorBuf.deallocate()
throw UniffiInternalError.unexpectedRustCallError
}
case CALL_UNEXPECTED_ERROR:
// When the rust code sees a panic, it tries to construct a RustBuffer
// with the message. But if that code panics, then it just sends back
// an empty buffer.
if callStatus.errorBuf.len > 0 {
throw UniffiInternalError.rustPanic(try FfiConverterString.lift(callStatus.errorBuf))
} else {
callStatus.errorBuf.deallocate()
throw UniffiInternalError.rustPanic("Rust panic")
}
case CALL_CANCELLED:
fatalError("Cancellation not supported yet")
default:
throw UniffiInternalError.unexpectedRustCallStatusCode
}
}
private func uniffiTraitInterfaceCall<T>(
callStatus: UnsafeMutablePointer<RustCallStatus>,
makeCall: () throws -> T,
writeReturn: (T) -> ()
) {
do {
try writeReturn(makeCall())
} catch let error {
callStatus.pointee.code = CALL_UNEXPECTED_ERROR
callStatus.pointee.errorBuf = FfiConverterString.lower(String(describing: error))
}
}
private func uniffiTraitInterfaceCallWithError<T, E>(
callStatus: UnsafeMutablePointer<RustCallStatus>,
makeCall: () throws -> T,
writeReturn: (T) -> (),
lowerError: (E) -> RustBuffer
) {
do {
try writeReturn(makeCall())
} catch let error as E {
callStatus.pointee.code = CALL_ERROR
callStatus.pointee.errorBuf = lowerError(error)
} catch {
callStatus.pointee.code = CALL_UNEXPECTED_ERROR
callStatus.pointee.errorBuf = FfiConverterString.lower(String(describing: error))
}
}
fileprivate final class UniffiHandleMap<T>: @unchecked Sendable {
// All mutation happens with this lock held, which is why we implement @unchecked Sendable.
private let lock = NSLock()
private var map: [UInt64: T] = [:]
private var currentHandle: UInt64 = 1
func insert(obj: T) -> UInt64 {
lock.withLock {
let handle = currentHandle
currentHandle += 1
map[handle] = obj
return handle
}
}
func get(handle: UInt64) throws -> T {
try lock.withLock {
guard let obj = map[handle] else {
throw UniffiInternalError.unexpectedStaleHandle
}
return obj
}
}
@discardableResult
func remove(handle: UInt64) throws -> T {
try lock.withLock {
guard let obj = map.removeValue(forKey: handle) else {
throw UniffiInternalError.unexpectedStaleHandle
}
return obj
}
}
var count: Int {
get {
map.count
}
}
}
// Public interface members begin here.
// Magic number for the Rust proxy to call using the same mechanism as every other method,
// to free the callback once it's dropped by Rust.
private let IDX_CALLBACK_FREE: Int32 = 0
// Callback return codes
private let UNIFFI_CALLBACK_SUCCESS: Int32 = 0
private let UNIFFI_CALLBACK_ERROR: Int32 = 1
private let UNIFFI_CALLBACK_UNEXPECTED_ERROR: Int32 = 2
#if swift(>=5.8)
@_documentation(visibility: private)
#endif
fileprivate struct FfiConverterUInt16: FfiConverterPrimitive {
typealias FfiType = UInt16
typealias SwiftType = UInt16
public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> UInt16 {
return try lift(readInt(&buf))
}
public static func write(_ value: SwiftType, into buf: inout [UInt8]) {
writeInt(&buf, lower(value))
}
}
#if swift(>=5.8)
@_documentation(visibility: private)
#endif
fileprivate struct FfiConverterUInt32: FfiConverterPrimitive {
typealias FfiType = UInt32
typealias SwiftType = UInt32
public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> UInt32 {
return try lift(readInt(&buf))
}
public static func write(_ value: SwiftType, into buf: inout [UInt8]) {
writeInt(&buf, lower(value))
}
}
#if swift(>=5.8)
@_documentation(visibility: private)
#endif
fileprivate struct FfiConverterString: FfiConverter {
typealias SwiftType = String
typealias FfiType = RustBuffer
public static func lift(_ value: RustBuffer) throws -> String {
defer {
value.deallocate()
}
if value.data == nil {
return String()
}
let bytes = UnsafeBufferPointer<UInt8>(start: value.data!, count: Int(value.len))
return String(bytes: bytes, encoding: String.Encoding.utf8)!
}
public static func lower(_ value: String) -> RustBuffer {
return value.utf8CString.withUnsafeBufferPointer { ptr in
// The swift string gives us int8_t, we want uint8_t.
ptr.withMemoryRebound(to: UInt8.self) { ptr in
// The swift string gives us a trailing null byte, we don't want it.
let buf = UnsafeBufferPointer(rebasing: ptr.prefix(upTo: ptr.count - 1))
return RustBuffer.from(buf)
}
}
}
public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> String {
let len: Int32 = try readInt(&buf)
return String(bytes: try readBytes(&buf, count: Int(len)), encoding: String.Encoding.utf8)!
}
public static func write(_ value: String, into buf: inout [UInt8]) {
let len = Int32(value.utf8.count)
writeInt(&buf, len)
writeBytes(&buf, value.utf8)
}
}
#if swift(>=5.8)
@_documentation(visibility: private)
#endif
fileprivate struct FfiConverterData: FfiConverterRustBuffer {
typealias SwiftType = Data
public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> Data {
let len: Int32 = try readInt(&buf)
return Data(try readBytes(&buf, count: Int(len)))
}
public static func write(_ value: Data, into buf: inout [UInt8]) {
let len = Int32(value.count)
writeInt(&buf, len)
writeBytes(&buf, value)
}
}
public protocol Backend: AnyObject, Sendable {
func sendRequest(request: Request, settings: ClientSettings) async throws -> Response
}
open class BackendImpl: Backend, @unchecked Sendable {
fileprivate let pointer: UnsafeMutableRawPointer!
/// Used to instantiate a [FFIObject] without an actual pointer, for fakes in tests, mostly.
#if swift(>=5.8)
@_documentation(visibility: private)
#endif
public struct NoPointer {
public init() {}
}
// TODO: We'd like this to be `private` but for Swifty reasons,
// we can't implement `FfiConverter` without making this `required` and we can't
// make it `required` without making it `public`.
#if swift(>=5.8)
@_documentation(visibility: private)
#endif
required public init(unsafeFromRawPointer pointer: UnsafeMutableRawPointer) {
self.pointer = pointer
}
// This constructor can be used to instantiate a fake object.
// - Parameter noPointer: Placeholder value so we can have a constructor separate from the default empty one that may be implemented for classes extending [FFIObject].
//
// - Warning:
// Any object instantiated with this constructor cannot be passed to an actual Rust-backed object. Since there isn't a backing [Pointer] the FFI lower functions will crash.
#if swift(>=5.8)
@_documentation(visibility: private)
#endif
public init(noPointer: NoPointer) {
self.pointer = nil
}
#if swift(>=5.8)
@_documentation(visibility: private)
#endif
public func uniffiClonePointer() -> UnsafeMutableRawPointer {
return try! rustCall { uniffi_viaduct_fn_clone_backend(self.pointer, $0) }
}
// No primary constructor declared for this class.
deinit {
guard let pointer = pointer else {
return
}
try! rustCall { uniffi_viaduct_fn_free_backend(pointer, $0) }
}
open func sendRequest(request: Request, settings: ClientSettings)async throws -> Response {
return
try await uniffiRustCallAsync(
rustFutureFunc: {
uniffi_viaduct_fn_method_backend_send_request(
self.uniffiClonePointer(),
FfiConverterTypeRequest_lower(request),FfiConverterTypeClientSettings_lower(settings)
)
},
pollFunc: ffi_viaduct_rust_future_poll_rust_buffer,
completeFunc: ffi_viaduct_rust_future_complete_rust_buffer,
freeFunc: ffi_viaduct_rust_future_free_rust_buffer,
liftFunc: FfiConverterTypeResponse_lift,
errorHandler: FfiConverterTypeViaductError_lift
)
}
}
// Put the implementation in a struct so we don't pollute the top-level namespace
fileprivate struct UniffiCallbackInterfaceBackend {
// Create the VTable using a series of closures.
// Swift automatically converts these into C callback functions.
//
// This creates 1-element array, since this seems to be the only way to construct a const
// pointer that we can pass to the Rust code.
static let vtable: [UniffiVTableCallbackInterfaceBackend] = [UniffiVTableCallbackInterfaceBackend(
sendRequest: { (
uniffiHandle: UInt64,
request: RustBuffer,
settings: RustBuffer,
uniffiFutureCallback: @escaping UniffiForeignFutureCompleteRustBuffer,
uniffiCallbackData: UInt64,
uniffiOutReturn: UnsafeMutablePointer<UniffiForeignFuture>
) in
let makeCall = {
() async throws -> Response in
guard let uniffiObj = try? FfiConverterTypeBackend.handleMap.get(handle: uniffiHandle) else {
throw UniffiInternalError.unexpectedStaleHandle
}
return try await uniffiObj.sendRequest(
request: try FfiConverterTypeRequest_lift(request),
settings: try FfiConverterTypeClientSettings_lift(settings)
)
}
let uniffiHandleSuccess = { (returnValue: Response) in
uniffiFutureCallback(
uniffiCallbackData,
UniffiForeignFutureStructRustBuffer(
returnValue: FfiConverterTypeResponse_lower(returnValue),
callStatus: RustCallStatus()
)
)
}
let uniffiHandleError = { (statusCode, errorBuf) in
uniffiFutureCallback(
uniffiCallbackData,
UniffiForeignFutureStructRustBuffer(
returnValue: RustBuffer.empty(),
callStatus: RustCallStatus(code: statusCode, errorBuf: errorBuf)
)
)
}
let uniffiForeignFuture = uniffiTraitInterfaceCallAsyncWithError(
makeCall: makeCall,
handleSuccess: uniffiHandleSuccess,
handleError: uniffiHandleError,
lowerError: FfiConverterTypeViaductError_lower
)
uniffiOutReturn.pointee = uniffiForeignFuture
},
uniffiFree: { (uniffiHandle: UInt64) -> () in
let result = try? FfiConverterTypeBackend.handleMap.remove(handle: uniffiHandle)
if result == nil {
print("Uniffi callback interface Backend: handle missing in uniffiFree")
}
}
)]
}
private func uniffiCallbackInitBackend() {
uniffi_viaduct_fn_init_callback_vtable_backend(UniffiCallbackInterfaceBackend.vtable)
}
#if swift(>=5.8)
@_documentation(visibility: private)
#endif
public struct FfiConverterTypeBackend: FfiConverter {
fileprivate static let handleMap = UniffiHandleMap<Backend>()
typealias FfiType = UnsafeMutableRawPointer
typealias SwiftType = Backend
public static func lift(_ pointer: UnsafeMutableRawPointer) throws -> Backend {
return BackendImpl(unsafeFromRawPointer: pointer)
}
public static func lower(_ value: Backend) -> UnsafeMutableRawPointer {
guard let ptr = UnsafeMutableRawPointer(bitPattern: UInt(truncatingIfNeeded: handleMap.insert(obj: value))) else {
fatalError("Cast to UnsafeMutableRawPointer failed")
}
return ptr
}
public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> Backend {
let v: UInt64 = try readInt(&buf)
// The Rust code won't compile if a pointer won't fit in a UInt64.
// We have to go via `UInt` because that's the thing that's the size of a pointer.
let ptr = UnsafeMutableRawPointer(bitPattern: UInt(truncatingIfNeeded: v))
if (ptr == nil) {
throw UniffiInternalError.unexpectedNullPointer
}
return try lift(ptr!)
}
public static func write(_ value: Backend, into buf: inout [UInt8]) {
// This fiddling is because `Int` is the thing that's the same size as a pointer.
// The Rust code won't compile if a pointer won't fit in a `UInt64`.
writeInt(&buf, UInt64(bitPattern: Int64(Int(bitPattern: lower(value)))))
}
}
#if swift(>=5.8)
@_documentation(visibility: private)
#endif
public func FfiConverterTypeBackend_lift(_ pointer: UnsafeMutableRawPointer) throws -> Backend {
return try FfiConverterTypeBackend.lift(pointer)
}
#if swift(>=5.8)
@_documentation(visibility: private)
#endif
public func FfiConverterTypeBackend_lower(_ value: Backend) -> UnsafeMutableRawPointer {
return FfiConverterTypeBackend.lower(value)
}
public struct ClientSettings {
public var timeout: UInt32
public var redirectLimit: UInt32
// Default memberwise initializers are never public by default, so we
// declare one manually.
public init(timeout: UInt32 = UInt32(0), redirectLimit: UInt32 = UInt32(10)) {
self.timeout = timeout
self.redirectLimit = redirectLimit
}
}
#if compiler(>=6)
extension ClientSettings: Sendable {}
#endif
extension ClientSettings: Equatable, Hashable {
public static func ==(lhs: ClientSettings, rhs: ClientSettings) -> Bool {
if lhs.timeout != rhs.timeout {
return false
}
if lhs.redirectLimit != rhs.redirectLimit {
return false
}
return true
}
public func hash(into hasher: inout Hasher) {
hasher.combine(timeout)
hasher.combine(redirectLimit)
}
}
#if swift(>=5.8)
@_documentation(visibility: private)
#endif
public struct FfiConverterTypeClientSettings: FfiConverterRustBuffer {
public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> ClientSettings {
return
try ClientSettings(
timeout: FfiConverterUInt32.read(from: &buf),
redirectLimit: FfiConverterUInt32.read(from: &buf)
)
}
public static func write(_ value: ClientSettings, into buf: inout [UInt8]) {
FfiConverterUInt32.write(value.timeout, into: &buf)
FfiConverterUInt32.write(value.redirectLimit, into: &buf)
}
}
#if swift(>=5.8)
@_documentation(visibility: private)
#endif
public func FfiConverterTypeClientSettings_lift(_ buf: RustBuffer) throws -> ClientSettings {
return try FfiConverterTypeClientSettings.lift(buf)
}
#if swift(>=5.8)
@_documentation(visibility: private)
#endif
public func FfiConverterTypeClientSettings_lower(_ value: ClientSettings) -> RustBuffer {
return FfiConverterTypeClientSettings.lower(value)
}
public struct Request {
public var method: Method
public var url: ViaductUrl
public var headers: Headers
public var body: Data?
// Default memberwise initializers are never public by default, so we
// declare one manually.
public init(method: Method, url: ViaductUrl, headers: Headers, body: Data?) {
self.method = method
self.url = url
self.headers = headers
self.body = body
}
}
#if compiler(>=6)
extension Request: Sendable {}
#endif
extension Request: Equatable, Hashable {
public static func ==(lhs: Request, rhs: Request) -> Bool {
if lhs.method != rhs.method {
return false
}
if lhs.url != rhs.url {
return false
}
if lhs.headers != rhs.headers {
return false
}
if lhs.body != rhs.body {
return false
}
return true
}
public func hash(into hasher: inout Hasher) {
hasher.combine(method)
hasher.combine(url)
hasher.combine(headers)
hasher.combine(body)
}
}
#if swift(>=5.8)
@_documentation(visibility: private)
#endif
public struct FfiConverterTypeRequest: FfiConverterRustBuffer {
public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> Request {
return
try Request(
method: FfiConverterTypeMethod.read(from: &buf),
url: FfiConverterTypeViaductUrl.read(from: &buf),
headers: FfiConverterTypeHeaders.read(from: &buf),
body: FfiConverterOptionData.read(from: &buf)
)
}
public static func write(_ value: Request, into buf: inout [UInt8]) {
FfiConverterTypeMethod.write(value.method, into: &buf)
FfiConverterTypeViaductUrl.write(value.url, into: &buf)
FfiConverterTypeHeaders.write(value.headers, into: &buf)
FfiConverterOptionData.write(value.body, into: &buf)
}
}
#if swift(>=5.8)
@_documentation(visibility: private)
#endif
public func FfiConverterTypeRequest_lift(_ buf: RustBuffer) throws -> Request {
return try FfiConverterTypeRequest.lift(buf)
}
#if swift(>=5.8)
@_documentation(visibility: private)
#endif
public func FfiConverterTypeRequest_lower(_ value: Request) -> RustBuffer {
return FfiConverterTypeRequest.lower(value)
}
/**
* A response from the server.
*/
public struct Response {
/**
* The method used to request this response.
*/
public var requestMethod: Method
/**
* The URL of this response.
*/
public var url: ViaductUrl
/**
* The HTTP Status code of this response.
*/
public var status: UInt16
/**
* The headers returned with this response.
*/
public var headers: Headers
/**
* The body of the response.
*/
public var body: Data
// Default memberwise initializers are never public by default, so we
// declare one manually.
public init(
/**
* The method used to request this response.
*/requestMethod: Method,
/**
* The URL of this response.
*/url: ViaductUrl,
/**
* The HTTP Status code of this response.
*/status: UInt16,
/**
* The headers returned with this response.
*/headers: Headers,
/**
* The body of the response.
*/body: Data) {
self.requestMethod = requestMethod
self.url = url
self.status = status
self.headers = headers
self.body = body
}
}
#if compiler(>=6)
extension Response: Sendable {}
#endif
extension Response: Equatable, Hashable {
public static func ==(lhs: Response, rhs: Response) -> Bool {
if lhs.requestMethod != rhs.requestMethod {
return false
}
if lhs.url != rhs.url {
return false
}
if lhs.status != rhs.status {
return false
}
if lhs.headers != rhs.headers {
return false
}
if lhs.body != rhs.body {
return false
}
return true
}
public func hash(into hasher: inout Hasher) {
hasher.combine(requestMethod)
hasher.combine(url)
hasher.combine(status)
hasher.combine(headers)
hasher.combine(body)
}
}
#if swift(>=5.8)
@_documentation(visibility: private)
#endif
public struct FfiConverterTypeResponse: FfiConverterRustBuffer {
public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> Response {
return
try Response(
requestMethod: FfiConverterTypeMethod.read(from: &buf),
url: FfiConverterTypeViaductUrl.read(from: &buf),
status: FfiConverterUInt16.read(from: &buf),
headers: FfiConverterTypeHeaders.read(from: &buf),
body: FfiConverterData.read(from: &buf)
)
}
public static func write(_ value: Response, into buf: inout [UInt8]) {
FfiConverterTypeMethod.write(value.requestMethod, into: &buf)
FfiConverterTypeViaductUrl.write(value.url, into: &buf)
FfiConverterUInt16.write(value.status, into: &buf)
FfiConverterTypeHeaders.write(value.headers, into: &buf)
FfiConverterData.write(value.body, into: &buf)
}
}
#if swift(>=5.8)
@_documentation(visibility: private)
#endif
public func FfiConverterTypeResponse_lift(_ buf: RustBuffer) throws -> Response {
return try FfiConverterTypeResponse.lift(buf)
}
#if swift(>=5.8)
@_documentation(visibility: private)
#endif
public func FfiConverterTypeResponse_lower(_ value: Response) -> RustBuffer {
return FfiConverterTypeResponse.lower(value)
}
// Note that we don't yet support `indirect` for enums.
// See https://github.com/mozilla/uniffi-rs/issues/396 for further discussion.
/**
* HTTP Methods.
*
* The supported methods are the limited to what's supported by android-components.
*/
public enum Method : UInt8 {
case get = 0
case head = 1
case post = 2
case put = 3
case delete = 4
case connect = 5
case options = 6
case trace = 7
case patch = 8
}
#if compiler(>=6)
extension Method: Sendable {}
#endif
#if swift(>=5.8)
@_documentation(visibility: private)
#endif
public struct FfiConverterTypeMethod: FfiConverterRustBuffer {
typealias SwiftType = Method
public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> Method {
let variant: Int32 = try readInt(&buf)
switch variant {
case 1: return .get
case 2: return .head
case 3: return .post
case 4: return .put
case 5: return .delete
case 6: return .connect
case 7: return .options
case 8: return .trace
case 9: return .patch
default: throw UniffiInternalError.unexpectedEnumCase
}
}
public static func write(_ value: Method, into buf: inout [UInt8]) {
switch value {
case .get:
writeInt(&buf, Int32(1))
case .head:
writeInt(&buf, Int32(2))
case .post:
writeInt(&buf, Int32(3))
case .put:
writeInt(&buf, Int32(4))
case .delete:
writeInt(&buf, Int32(5))
case .connect:
writeInt(&buf, Int32(6))
case .options:
writeInt(&buf, Int32(7))
case .trace:
writeInt(&buf, Int32(8))
case .patch:
writeInt(&buf, Int32(9))
}
}
}
#if swift(>=5.8)
@_documentation(visibility: private)
#endif
public func FfiConverterTypeMethod_lift(_ buf: RustBuffer) throws -> Method {
return try FfiConverterTypeMethod.lift(buf)
}
#if swift(>=5.8)
@_documentation(visibility: private)
#endif
public func FfiConverterTypeMethod_lower(_ value: Method) -> RustBuffer {
return FfiConverterTypeMethod.lower(value)
}
extension Method: Equatable, Hashable {}
public enum ViaductError: Swift.Error {
case RequestHeaderError(String
)
case BackendError(String
)
case NetworkError(String
)
case BackendAlreadyInitialized
case BackendNotInitialized
case SetBackendError
/**
* Note: we return this if the server returns a bad URL with
* its response. This *probably* should never happen, but who knows.
*/
case UrlError(String
)
case NonTlsUrl
}
#if swift(>=5.8)
@_documentation(visibility: private)
#endif
public struct FfiConverterTypeViaductError: FfiConverterRustBuffer {
typealias SwiftType = ViaductError
public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> ViaductError {
let variant: Int32 = try readInt(&buf)
switch variant {
case 1: return .RequestHeaderError(
try FfiConverterString.read(from: &buf)
)
case 2: return .BackendError(
try FfiConverterString.read(from: &buf)
)
case 3: return .NetworkError(
try FfiConverterString.read(from: &buf)
)
case 4: return .BackendAlreadyInitialized
case 5: return .BackendNotInitialized
case 6: return .SetBackendError
case 7: return .UrlError(
try FfiConverterString.read(from: &buf)
)
case 8: return .NonTlsUrl
default: throw UniffiInternalError.unexpectedEnumCase
}
}
public static func write(_ value: ViaductError, into buf: inout [UInt8]) {
switch value {
case let .RequestHeaderError(v1):
writeInt(&buf, Int32(1))
FfiConverterString.write(v1, into: &buf)
case let .BackendError(v1):
writeInt(&buf, Int32(2))
FfiConverterString.write(v1, into: &buf)
case let .NetworkError(v1):
writeInt(&buf, Int32(3))
FfiConverterString.write(v1, into: &buf)
case .BackendAlreadyInitialized:
writeInt(&buf, Int32(4))
case .BackendNotInitialized:
writeInt(&buf, Int32(5))
case .SetBackendError:
writeInt(&buf, Int32(6))
case let .UrlError(v1):
writeInt(&buf, Int32(7))
FfiConverterString.write(v1, into: &buf)
case .NonTlsUrl:
writeInt(&buf, Int32(8))
}
}
}
#if swift(>=5.8)
@_documentation(visibility: private)
#endif
public func FfiConverterTypeViaductError_lift(_ buf: RustBuffer) throws -> ViaductError {
return try FfiConverterTypeViaductError.lift(buf)
}
#if swift(>=5.8)
@_documentation(visibility: private)
#endif
public func FfiConverterTypeViaductError_lower(_ value: ViaductError) -> RustBuffer {
return FfiConverterTypeViaductError.lower(value)
}
extension ViaductError: Equatable, Hashable {}
extension ViaductError: Foundation.LocalizedError {
public var errorDescription: String? {
String(reflecting: self)
}
}
#if swift(>=5.8)
@_documentation(visibility: private)
#endif
fileprivate struct FfiConverterOptionData: FfiConverterRustBuffer {
typealias SwiftType = Data?
public static func write(_ value: SwiftType, into buf: inout [UInt8]) {
guard let value = value else {
writeInt(&buf, Int8(0))
return
}
writeInt(&buf, Int8(1))
FfiConverterData.write(value, into: &buf)
}
public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> SwiftType {
switch try readInt(&buf) as Int8 {
case 0: return nil
case 1: return try FfiConverterData.read(from: &buf)
default: throw UniffiInternalError.unexpectedOptionalTag
}
}
}
#if swift(>=5.8)
@_documentation(visibility: private)
#endif
fileprivate struct FfiConverterDictionaryStringString: FfiConverterRustBuffer {
public static func write(_ value: [String: String], into buf: inout [UInt8]) {
let len = Int32(value.count)
writeInt(&buf, len)
for (key, value) in value {
FfiConverterString.write(key, into: &buf)
FfiConverterString.write(value, into: &buf)
}
}
public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> [String: String] {
let len: Int32 = try readInt(&buf)
var dict = [String: String]()
dict.reserveCapacity(Int(len))
for _ in 0..<len {
let key = try FfiConverterString.read(from: &buf)
let value = try FfiConverterString.read(from: &buf)
dict[key] = value
}
return dict
}
}
/**
* Typealias from the type name used in the UDL file to the builtin type. This
* is needed because the UDL type name is used in function/method signatures.
*/
public typealias Headers = [String: String]
#if swift(>=5.8)
@_documentation(visibility: private)
#endif
public struct FfiConverterTypeHeaders: FfiConverter {
public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> Headers {
return try FfiConverterDictionaryStringString.read(from: &buf)
}
public static func write(_ value: Headers, into buf: inout [UInt8]) {
return FfiConverterDictionaryStringString.write(value, into: &buf)
}
public static func lift(_ value: RustBuffer) throws -> Headers {
return try FfiConverterDictionaryStringString.lift(value)
}
public static func lower(_ value: Headers) -> RustBuffer {
return FfiConverterDictionaryStringString.lower(value)
}
}
#if swift(>=5.8)
@_documentation(visibility: private)
#endif
public func FfiConverterTypeHeaders_lift(_ value: RustBuffer) throws -> Headers {
return try FfiConverterTypeHeaders.lift(value)
}
#if swift(>=5.8)
@_documentation(visibility: private)
#endif
public func FfiConverterTypeHeaders_lower(_ value: Headers) -> RustBuffer {
return FfiConverterTypeHeaders.lower(value)
}
/**
* Typealias from the type name used in the UDL file to the builtin type. This
* is needed because the UDL type name is used in function/method signatures.
*/
public typealias ViaductUrl = String
#if swift(>=5.8)
@_documentation(visibility: private)
#endif
public struct FfiConverterTypeViaductUrl: FfiConverter {
public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> ViaductUrl {
return try FfiConverterString.read(from: &buf)
}
public static func write(_ value: ViaductUrl, into buf: inout [UInt8]) {
return FfiConverterString.write(value, into: &buf)
}
public static func lift(_ value: RustBuffer) throws -> ViaductUrl {
return try FfiConverterString.lift(value)
}
public static func lower(_ value: ViaductUrl) -> RustBuffer {
return FfiConverterString.lower(value)
}
}
#if swift(>=5.8)
@_documentation(visibility: private)
#endif
public func FfiConverterTypeViaductUrl_lift(_ value: RustBuffer) throws -> ViaductUrl {
return try FfiConverterTypeViaductUrl.lift(value)
}
#if swift(>=5.8)
@_documentation(visibility: private)
#endif
public func FfiConverterTypeViaductUrl_lower(_ value: ViaductUrl) -> RustBuffer {
return FfiConverterTypeViaductUrl.lower(value)
}
private let UNIFFI_RUST_FUTURE_POLL_READY: Int8 = 0
private let UNIFFI_RUST_FUTURE_POLL_MAYBE_READY: Int8 = 1
fileprivate let uniffiContinuationHandleMap = UniffiHandleMap<UnsafeContinuation<Int8, Never>>()
fileprivate func uniffiRustCallAsync<F, T>(
rustFutureFunc: () -> UInt64,
pollFunc: (UInt64, @escaping UniffiRustFutureContinuationCallback, UInt64) -> (),
completeFunc: (UInt64, UnsafeMutablePointer<RustCallStatus>) -> F,
freeFunc: (UInt64) -> (),
liftFunc: (F) throws -> T,
errorHandler: ((RustBuffer) throws -> Swift.Error)?
) async throws -> T {
// Make sure to call the ensure init function since future creation doesn't have a
// RustCallStatus param, so doesn't use makeRustCall()
uniffiEnsureViaductInitialized()
let rustFuture = rustFutureFunc()
defer {
freeFunc(rustFuture)
}
var pollResult: Int8;
repeat {
pollResult = await withUnsafeContinuation {
pollFunc(
rustFuture,
uniffiFutureContinuationCallback,
uniffiContinuationHandleMap.insert(obj: $0)
)
}
} while pollResult != UNIFFI_RUST_FUTURE_POLL_READY
return try liftFunc(makeRustCall(
{ completeFunc(rustFuture, $0) },
errorHandler: errorHandler
))
}
// Callback handlers for an async calls. These are invoked by Rust when the future is ready. They
// lift the return value or error and resume the suspended function.
fileprivate func uniffiFutureContinuationCallback(handle: UInt64, pollResult: Int8) {
if let continuation = try? uniffiContinuationHandleMap.remove(handle: handle) {
continuation.resume(returning: pollResult)
} else {
print("uniffiFutureContinuationCallback invalid handle")
}
}
private func uniffiTraitInterfaceCallAsync<T>(
makeCall: @escaping () async throws -> T,
handleSuccess: @escaping (T) -> (),
handleError: @escaping (Int8, RustBuffer) -> ()
) -> UniffiForeignFuture {
let task = Task {
do {
handleSuccess(try await makeCall())
} catch {
handleError(CALL_UNEXPECTED_ERROR, FfiConverterString.lower(String(describing: error)))
}
}
let handle = UNIFFI_FOREIGN_FUTURE_HANDLE_MAP.insert(obj: task)
return UniffiForeignFuture(handle: handle, free: uniffiForeignFutureFree)
}
private func uniffiTraitInterfaceCallAsyncWithError<T, E>(
makeCall: @escaping () async throws -> T,
handleSuccess: @escaping (T) -> (),
handleError: @escaping (Int8, RustBuffer) -> (),
lowerError: @escaping (E) -> RustBuffer
) -> UniffiForeignFuture {
let task = Task {
do {
handleSuccess(try await makeCall())
} catch let error as E {
handleError(CALL_ERROR, lowerError(error))
} catch {
handleError(CALL_UNEXPECTED_ERROR, FfiConverterString.lower(String(describing: error)))
}
}
let handle = UNIFFI_FOREIGN_FUTURE_HANDLE_MAP.insert(obj: task)
return UniffiForeignFuture(handle: handle, free: uniffiForeignFutureFree)
}
// Borrow the callback handle map implementation to store foreign future handles
// TODO: consolidate the handle-map code (https://github.com/mozilla/uniffi-rs/pull/1823)
fileprivate let UNIFFI_FOREIGN_FUTURE_HANDLE_MAP = UniffiHandleMap<UniffiForeignFutureTask>()
// Protocol for tasks that handle foreign futures.
//
// Defining a protocol allows all tasks to be stored in the same handle map. This can't be done
// with the task object itself, since has generic parameters.
fileprivate protocol UniffiForeignFutureTask {
func cancel()
}
extension Task: UniffiForeignFutureTask {}
private func uniffiForeignFutureFree(handle: UInt64) {
do {
let task = try UNIFFI_FOREIGN_FUTURE_HANDLE_MAP.remove(handle: handle)
// Set the cancellation flag on the task. If it's still running, the code can check the
// cancellation flag or call `Task.checkCancellation()`. If the task has completed, this is
// a no-op.
task.cancel()
} catch {
print("uniffiForeignFutureFree: handle missing from handlemap")
}
}
// For testing
public func uniffiForeignFutureHandleCountViaduct() -> Int {
UNIFFI_FOREIGN_FUTURE_HANDLE_MAP.count
}
/**
* Allow non-HTTPS requests to the emulator loopback URL
*/
public func allowAndroidEmulatorLoopback() {try! rustCall() {
uniffi_viaduct_fn_func_allow_android_emulator_loopback($0
)
}
}
public func initBackend(backend: Backend)throws {try rustCallWithError(FfiConverterTypeViaductError_lift) {
uniffi_viaduct_fn_func_init_backend(
FfiConverterTypeBackend_lower(backend),$0
)
}
}
private enum InitializationResult {
case ok
case contractVersionMismatch
case apiChecksumMismatch
}
// Use a global variable to perform the versioning checks. Swift ensures that
// the code inside is only computed once.
private let initializationResult: InitializationResult = {
// Get the bindings contract version from our ComponentInterface
let bindings_contract_version = 29
// Get the scaffolding contract version by calling the into the dylib
let scaffolding_contract_version = ffi_viaduct_uniffi_contract_version()
if bindings_contract_version != scaffolding_contract_version {
return InitializationResult.contractVersionMismatch
}
if (uniffi_viaduct_checksum_func_allow_android_emulator_loopback() != 38617) {
return InitializationResult.apiChecksumMismatch
}
if (uniffi_viaduct_checksum_func_init_backend() != 21801) {
return InitializationResult.apiChecksumMismatch
}
if (uniffi_viaduct_checksum_method_backend_send_request() != 4029) {
return InitializationResult.apiChecksumMismatch
}
uniffiCallbackInitBackend()
return InitializationResult.ok
}()
// Make the ensure init function public so that other modules which have external type references to
// our types can call it.
public func uniffiEnsureViaductInitialized() {
switch initializationResult {
case .ok:
break
case .contractVersionMismatch:
fatalError("UniFFI contract version mismatch: try cleaning and rebuilding your project")
case .apiChecksumMismatch:
fatalError("UniFFI API checksum mismatch: try cleaning and rebuilding your project")
}
}
// swiftlint:enable all