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)
}
init(rawBufferPointer: UnsafeRawBufferPointer) {
self.init(
len: Int32(rawBufferPointer.count),
data: rawBufferPointer.baseAddress?.assumingMemoryBound(to: UInt8.self)
)
}
}
// Converter for `&[u8]` / `[ByRef] bytes` arguments.
//
// Conforms to `FfiConverter` so the compiler enforces the full converter
// method set. Only the scope-bound `lower(_:_body:)` overload is sound —
// zero-copy byte buffers only flow foreign -> Rust, and only in argument
// position. The four protocol-witness methods (`lift`, `lower`, `read`,
// `write`) `fatalError` at runtime if anyone reaches them.
//
// The scope-bound `lower` takes a closure because the `ForeignBytes`
// pointer is only guaranteed valid for the duration of
// `Data.withUnsafeBytes`. Callers must run the full FFI call inside
// the closure body.
fileprivate enum FfiConverterByRefBytes: FfiConverter {
typealias SwiftType = Data
typealias FfiType = ForeignBytes
static func lower<R>(_ value: Data, _ body: (ForeignBytes) throws -> R) rethrows -> R {
return try value.withUnsafeBytes { rawBuf in
try body(ForeignBytes(rawBufferPointer: rawBuf))
}
}
static func lower(_ value: Data) -> ForeignBytes {
fatalError("ByRef bytes cannot use the plain lower: returning ForeignBytes escapes the Data.withUnsafeBytes scope. Use the scope-bound lower(_:_body:) overload instead.")
}
static func lift(_ value: ForeignBytes) throws -> Data {
fatalError("ByRef bytes cannot be lifted: zero-copy &[u8] only flows foreign->Rust")
}
static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> Data {
fatalError("ByRef bytes cannot be read from a buffer: zero-copy &[u8] is only supported in argument position, not nested in records/options/etc.")
}
static func write(_ value: Data, into buf: inout [UInt8]) {
fatalError("ByRef bytes cannot be written to a buffer: zero-copy &[u8] is only supported in argument position, not nested in records/options/etc.")
}
}
// 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))
}
}
// Initial value and increment amount for handles.
// These ensure that SWIFT handles always have the lowest bit set
fileprivate let UNIFFI_HANDLEMAP_INITIAL: UInt64 = 1
fileprivate let UNIFFI_HANDLEMAP_DELTA: UInt64 = 2
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 = UNIFFI_HANDLEMAP_INITIAL
func insert(obj: T) -> UInt64 {
lock.withLock {
return doInsert(obj)
}
}
// Low-level insert function, this assumes `lock` is held.
private func doInsert(_ obj: T) -> UInt64 {
let handle = currentHandle
currentHandle += UNIFFI_HANDLEMAP_DELTA
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
}
}
func clone(handle: UInt64) throws -> UInt64 {
try lock.withLock {
guard let obj = map[handle] else {
throw UniffiInternalError.unexpectedStaleHandle
}
return doInsert(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))
// Use Swift's native UTF-8 decoder; `String(bytes:encoding:.utf8)` goes
// through Foundation's NSString and silently strips a leading U+FEFF BOM.
// Invalid UTF-8 substitutes U+FFFD instead of trapping (unreachable
// given Rust's `String` invariant).
return String(decoding: bytes, as: UTF8.self)
}
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)
// See `lift` above for why we avoid Foundation's NSString-backed decoder here.
return String(decoding: try readBytes(&buf, count: Int(len)), as: UTF8.self)
}
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 handle: UInt64
/// Used to instantiate a [FFIObject] without an actual handle, for fakes in tests, mostly.
#if swift(>=5.8)
@_documentation(visibility: private)
#endif
public struct NoHandle {
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(unsafeFromHandle handle: UInt64) {
self.handle = handle
}
// This constructor can be used to instantiate a fake object.
// - Parameter noHandle: 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 handle the FFI lower functions will crash.
#if swift(>=5.8)
@_documentation(visibility: private)
#endif
public init(noHandle: NoHandle) {
self.handle = 0
}
#if swift(>=5.8)
@_documentation(visibility: private)
#endif
public func uniffiCloneHandle() -> UInt64 {
return try! rustCall { uniffi_viaduct_fn_clone_backend(self.handle, $0) }
}
// No primary constructor declared for this class.
deinit {
if handle == 0 {
// Mock objects have handle=0 don't try to free them
return
}
try! rustCall { uniffi_viaduct_fn_free_backend(handle, $0) }
}
open func sendRequest(request: Request, settings: ClientSettings)async throws -> Response {
return
try await uniffiRustCallAsync(
rustFutureFunc: {
uniffi_viaduct_fn_method_backend_send_request(
self.uniffiCloneHandle(),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.
//
// Store the vtable directly.
static let vtable: UniffiVTableCallbackInterfaceBackend = UniffiVTableCallbackInterfaceBackend(
uniffiFree: { (uniffiHandle: UInt64) -> () in
do {
try FfiConverterTypeBackend.handleMap.remove(handle: uniffiHandle)
} catch {
print("Uniffi callback interface Backend: handle missing in uniffiFree")
}
},
uniffiClone: { (uniffiHandle: UInt64) -> UInt64 in
do {
return try FfiConverterTypeBackend.handleMap.clone(handle: uniffiHandle)
} catch {
fatalError("Uniffi callback interface Backend: handle missing in uniffiClone")
}
},
sendRequest: { (
uniffiHandle: UInt64,
request: RustBuffer,
settings: RustBuffer,
uniffiFutureCallback: @escaping UniffiForeignFutureCompleteRustBuffer,
uniffiCallbackData: UInt64,
uniffiOutDroppedCallback: UnsafeMutablePointer<UniffiForeignFutureDroppedCallbackStruct>
) 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,
UniffiForeignFutureResultRustBuffer(
returnValue: FfiConverterTypeResponse_lower(returnValue),
callStatus: RustCallStatus()
)
)
}
let uniffiHandleError = { (statusCode, errorBuf) in
uniffiFutureCallback(
uniffiCallbackData,
UniffiForeignFutureResultRustBuffer(
returnValue: RustBuffer.empty(),
callStatus: RustCallStatus(code: statusCode, errorBuf: errorBuf)
)
)
}
uniffiTraitInterfaceCallAsyncWithError(
makeCall: makeCall,
handleSuccess: uniffiHandleSuccess,
handleError: uniffiHandleError,
lowerError: FfiConverterTypeViaductError_lower,
droppedCallback: uniffiOutDroppedCallback
)
}
)
// Rust stores this pointer for future callback invocations, so it must live
// for the process lifetime (not just for the init function call).
//
// `nonisolated(unsafe)` is needed under Swift 6 strict concurrency.
// This is safe because the pointee is initialized once during static init
// and never mutated by either side of the FFI. Its fields are C function pointers.
nonisolated(unsafe) static let vtablePtr: UnsafePointer<UniffiVTableCallbackInterfaceBackend> = {
let ptr = UnsafeMutablePointer<UniffiVTableCallbackInterfaceBackend>.allocate(capacity: 1)
ptr.initialize(to: vtable)
return UnsafePointer(ptr)
}()
}
private func uniffiCallbackInitBackend() {
uniffi_viaduct_fn_init_callback_vtable_backend(UniffiCallbackInterfaceBackend.vtablePtr)
}
#if swift(>=5.8)
@_documentation(visibility: private)
#endif
public struct FfiConverterTypeBackend: FfiConverter {
fileprivate static let handleMap = UniffiHandleMap<Backend>()
typealias FfiType = UInt64
typealias SwiftType = Backend
public static func lift(_ handle: UInt64) throws -> Backend {
if ((handle & 1) == 0) {
// Rust-generated handle, construct a new class that uses the handle to implement the
// interface
return BackendImpl(unsafeFromHandle: handle)
} else {
// Swift-generated handle, get the object from the handle map
return try handleMap.remove(handle: handle)
}
}
public static func lower(_ value: Backend) -> UInt64 {
if let rustImpl = value as? BackendImpl {
// Rust-implemented object. Clone the handle and return it
return rustImpl.uniffiCloneHandle()
} else {
// Swift object, generate a new vtable handle and return that.
return handleMap.insert(obj: value)
}
}
public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> Backend {
let handle: UInt64 = try readInt(&buf)
return try lift(handle)
}
public static func write(_ value: Backend, into buf: inout [UInt8]) {
writeInt(&buf, lower(value))
}
}
#if swift(>=5.8)
@_documentation(visibility: private)
#endif
public func FfiConverterTypeBackend_lift(_ handle: UInt64) throws -> Backend {
return try FfiConverterTypeBackend.lift(handle)
}
#if swift(>=5.8)
@_documentation(visibility: private)
#endif
public func FfiConverterTypeBackend_lower(_ value: Backend) -> UInt64 {
return FfiConverterTypeBackend.lower(value)
}
public struct ClientSettings: Equatable, Hashable {
/**
* Timeout for the entire request in ms (0 indicates no timeout).
*/
public var timeout: UInt32
/**
* Maximum amount of redirects to follow (0 means redirects are not allowed)
*/
public var redirectLimit: UInt32
/**
* OHTTP channel to use for all requests (if any)
*/
public var ohttpChannel: String?
/**
* Client default user-agent.
*
* This overrides the global default user-agent and is used when no `User-agent` header is set
* directly in the Request.
*/
public var userAgent: String?
// Default memberwise initializers are never public by default, so we
// declare one manually.
public init(
/**
* Timeout for the entire request in ms (0 indicates no timeout).
*/timeout: UInt32 = UInt32(60000),
/**
* Maximum amount of redirects to follow (0 means redirects are not allowed)
*/redirectLimit: UInt32 = UInt32(10),
/**
* OHTTP channel to use for all requests (if any)
*/ohttpChannel: String?,
/**
* Client default user-agent.
*
* This overrides the global default user-agent and is used when no `User-agent` header is set
* directly in the Request.
*/userAgent: String? = nil) {
self.timeout = timeout
self.redirectLimit = redirectLimit
self.ohttpChannel = ohttpChannel
self.userAgent = userAgent
}
}
#if compiler(>=6)
extension ClientSettings: Sendable {}
#endif
#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),
ohttpChannel: FfiConverterOptionString.read(from: &buf),
userAgent: FfiConverterOptionString.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)
FfiConverterOptionString.write(value.ohttpChannel, into: &buf)
FfiConverterOptionString.write(value.userAgent, 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)
}
/**
* Configuration for an OHTTP channel
*/
public struct OhttpConfig: Equatable, Hashable {
/**
* The relay URL that will proxy requests
*/
public var relayUrl: String
/**
* The gateway host that provides encryption keys and decrypts requests
*/
public var gatewayHost: String
// Default memberwise initializers are never public by default, so we
// declare one manually.
public init(
/**
* The relay URL that will proxy requests
*/relayUrl: String,
/**
* The gateway host that provides encryption keys and decrypts requests
*/gatewayHost: String) {
self.relayUrl = relayUrl
self.gatewayHost = gatewayHost
}
}
#if compiler(>=6)
extension OhttpConfig: Sendable {}
#endif
#if swift(>=5.8)
@_documentation(visibility: private)
#endif
public struct FfiConverterTypeOhttpConfig: FfiConverterRustBuffer {
public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> OhttpConfig {
return
try OhttpConfig(
relayUrl: FfiConverterString.read(from: &buf),
gatewayHost: FfiConverterString.read(from: &buf)
)
}
public static func write(_ value: OhttpConfig, into buf: inout [UInt8]) {
FfiConverterString.write(value.relayUrl, into: &buf)
FfiConverterString.write(value.gatewayHost, into: &buf)
}
}
#if swift(>=5.8)
@_documentation(visibility: private)
#endif
public func FfiConverterTypeOhttpConfig_lift(_ buf: RustBuffer) throws -> OhttpConfig {
return try FfiConverterTypeOhttpConfig.lift(buf)
}
#if swift(>=5.8)
@_documentation(visibility: private)
#endif
public func FfiConverterTypeOhttpConfig_lower(_ value: OhttpConfig) -> RustBuffer {
return FfiConverterTypeOhttpConfig.lower(value)
}
public struct Request: Equatable, Hashable {
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
#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: Equatable, Hashable {
/**
* 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
#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)
}
/**
* HTTP Methods.
*
* The supported methods are the limited to what's supported by android-components.
*/
public enum Method: UInt8, Equatable, Hashable {
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)
}
public
enum ViaductError: Swift.Error, Equatable, Hashable, Foundation.LocalizedError {
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
case OhttpChannelNotConfigured(String
)
case OhttpConfigFetchFailed(String
)
case OhttpRequestError(String
)
case OhttpResponseError(String
)
case OhttpNotSupported
public var errorDescription: String? {
String(reflecting: self)
}
}
#if compiler(>=6)
extension ViaductError: Sendable {}
#endif
#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
case 9: return .OhttpChannelNotConfigured(
try FfiConverterString.read(from: &buf)
)
case 10: return .OhttpConfigFetchFailed(
try FfiConverterString.read(from: &buf)
)
case 11: return .OhttpRequestError(
try FfiConverterString.read(from: &buf)
)
case 12: return .OhttpResponseError(
try FfiConverterString.read(from: &buf)
)
case 13: return .OhttpNotSupported
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))
case let .OhttpChannelNotConfigured(v1):
writeInt(&buf, Int32(9))
FfiConverterString.write(v1, into: &buf)
case let .OhttpConfigFetchFailed(v1):
writeInt(&buf, Int32(10))
FfiConverterString.write(v1, into: &buf)
case let .OhttpRequestError(v1):
writeInt(&buf, Int32(11))
FfiConverterString.write(v1, into: &buf)
case let .OhttpResponseError(v1):
writeInt(&buf, Int32(12))
FfiConverterString.write(v1, into: &buf)
case .OhttpNotSupported:
writeInt(&buf, Int32(13))
}
}
}
#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)
}
#if swift(>=5.8)
@_documentation(visibility: private)
#endif
fileprivate struct FfiConverterOptionString: FfiConverterRustBuffer {
typealias SwiftType = String?
public static func write(_ value: SwiftType, into buf: inout [UInt8]) {
guard let value = value else {
writeInt(&buf, Int8(0))
return
}
writeInt(&buf, Int8(1))
FfiConverterString.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 FfiConverterString.read(from: &buf)
default: throw UniffiInternalError.unexpectedOptionalTag
}
}
}
#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 FfiConverterSequenceString: FfiConverterRustBuffer {
typealias SwiftType = [String]
public static func write(_ value: [String], into buf: inout [UInt8]) {
let len = Int32(value.count)
writeInt(&buf, len)
for item in value {
FfiConverterString.write(item, into: &buf)
}
}
public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> [String] {
let len: Int32 = try readInt(&buf)
var seq = [String]()
seq.reserveCapacity(Int(len))
for _ in 0 ..< len {
seq.append(try FfiConverterString.read(from: &buf))
}
return seq
}
}
#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
}
}
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)
}
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_WAKE: 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,
{ handle, pollResult in
uniffiFutureContinuationCallback(handle: handle, pollResult: pollResult)
},
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) -> (),
droppedCallback: UnsafeMutablePointer<UniffiForeignFutureDroppedCallbackStruct>
) {
let task = Task {
// Note: it's important we call either `handleSuccess` or `handleError` exactly once. Each
// call consumes an Arc reference, which means there should be no possibility of a double
// call. The following code is structured so that will will never call both `handleSuccess`
// and `handleError`, even in the face of weird errors.
//
// On platforms that need extra machinery to make C-ABI calls, like JNA or ctypes, it's
// possible that we fail to make either call. However, it doesn't seem like this is
// possible on Swift since swift can just make the C call directly.
var callResult: T
do {
callResult = try await makeCall()
} catch {
handleError(CALL_UNEXPECTED_ERROR, FfiConverterString.lower(String(describing: error)))
return
}
handleSuccess(callResult)
}
let handle = UNIFFI_FOREIGN_FUTURE_HANDLE_MAP.insert(obj: task)
droppedCallback.pointee = UniffiForeignFutureDroppedCallbackStruct(
handle: handle,
free: uniffiForeignFutureDroppedCallback
)
}
private func uniffiTraitInterfaceCallAsyncWithError<T, E>(
makeCall: @escaping () async throws -> T,
handleSuccess: @escaping (T) -> (),
handleError: @escaping (Int8, RustBuffer) -> (),
lowerError: @escaping (E) -> RustBuffer,
droppedCallback: UnsafeMutablePointer<UniffiForeignFutureDroppedCallbackStruct>
) {
let task = Task {
// See the note in uniffiTraitInterfaceCallAsync for details on `handleSuccess` and
// `handleError`.
var callResult: T
do {
callResult = try await makeCall()
} catch let error as E {
handleError(CALL_ERROR, lowerError(error))
return
} catch {
handleError(CALL_UNEXPECTED_ERROR, FfiConverterString.lower(String(describing: error)))
return
}
handleSuccess(callResult)
}
let handle = UNIFFI_FOREIGN_FUTURE_HANDLE_MAP.insert(obj: task)
droppedCallback.pointee = UniffiForeignFutureDroppedCallbackStruct(
handle: handle,
free: uniffiForeignFutureDroppedCallback
)
}
// Borrow the callback handle map implementation to store foreign future handles
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 uniffiForeignFutureDroppedCallback(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("uniffiForeignFutureDroppedCallback: handle missing from handlemap")
}
}
// For testing
public func uniffiForeignFutureHandleCountViaduct() -> Int {
UNIFFI_FOREIGN_FUTURE_HANDLE_MAP.count
}
/**
* Send a request through an OHTTP channel.
*
* This encrypts the request and routes it through the configured OHTTP
* relay/gateway for the specified channel.
*
* # Arguments
* * `request` - The request to send
* * `channel` - The name of the OHTTP channel to use (e.g., "merino")
*
* # Example (Kotlin)
* ```kotlin
* val response = sendOhttpRequest(
* Request(
* method = Method.GET,
* headers = mapOf("Accept" to "application/json"),
* body = null
* ),
* "merino"
* )
* ```
*/
public func sendOhttpRequest(request: Request, channel: String)async throws -> Response {
return
try await uniffiRustCallAsync(
rustFutureFunc: {
uniffi_viaduct_fn_func_send_ohttp_request(FfiConverterTypeRequest_lower(request),FfiConverterString.lower(channel)
)
},
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
)
}
public func initBackend(backend: Backend) {try! rustCall() {
uniffiCallStatus in
uniffi_viaduct_fn_func_init_backend(
FfiConverterTypeBackend_lower(backend),uniffiCallStatus
)
}
}
/**
* Clear all OHTTP channel configurations
*/
public func clearOhttpChannels() {try! rustCall() {
uniffiCallStatus in
uniffi_viaduct_fn_func_clear_ohttp_channels(uniffiCallStatus
)
}
}
/**
* Configure default OHTTP channels for common Mozilla services
* This sets up:
* - "relay1": For general telemetry and services through Mozilla's shared gateway
* - "merino": For Firefox Suggest recommendations through Merino's dedicated relay/gateway
*/
public func configureDefaultOhttpChannels()throws {try rustCallWithError(FfiConverterTypeViaductError_lift) {
uniffiCallStatus in
uniffi_viaduct_fn_func_configure_default_ohttp_channels(uniffiCallStatus
)
}
}
/**
* Configure an OHTTP channel with the given configuration
* If an existing OHTTP config exists with the same name, it will be overwritten
*/
public func configureOhttpChannel(channel: String, config: OhttpConfig)throws {try rustCallWithError(FfiConverterTypeViaductError_lift) {
uniffiCallStatus in
uniffi_viaduct_fn_func_configure_ohttp_channel(
FfiConverterString.lower(channel),
FfiConverterTypeOhttpConfig_lower(config),uniffiCallStatus
)
}
}
/**
* List all configured OHTTP channels
*/
public func listOhttpChannels() -> [String] {
return try! FfiConverterSequenceString.lift(try! rustCall() {
uniffiCallStatus in
uniffi_viaduct_fn_func_list_ohttp_channels(uniffiCallStatus
)
})
}
/**
* Allow non-HTTPS requests to the emulator loopback URL
*/
public func allowAndroidEmulatorLoopback() {try! rustCall() {
uniffiCallStatus in
uniffi_viaduct_fn_func_allow_android_emulator_loopback(uniffiCallStatus
)
}
}
/**
* Set the global default user-agent
*
* This is what's used when no user-agent is set in the `ClientSettings` and no `user-agent`
* header is set in the Request.
*/
public func setGlobalDefaultUserAgent(userAgent: String) {try! rustCall() {
uniffiCallStatus in
uniffi_viaduct_fn_func_set_global_default_user_agent(
FfiConverterString.lower(userAgent),uniffiCallStatus
)
}
}
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 = 30
// 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_send_ohttp_request() != 60356) {
return InitializationResult.apiChecksumMismatch
}
if (uniffi_viaduct_checksum_func_init_backend() != 34397) {
return InitializationResult.apiChecksumMismatch
}
if (uniffi_viaduct_checksum_func_clear_ohttp_channels() != 21471) {
return InitializationResult.apiChecksumMismatch
}
if (uniffi_viaduct_checksum_func_configure_default_ohttp_channels() != 48511) {
return InitializationResult.apiChecksumMismatch
}
if (uniffi_viaduct_checksum_func_configure_ohttp_channel() != 33688) {
return InitializationResult.apiChecksumMismatch
}
if (uniffi_viaduct_checksum_func_list_ohttp_channels() != 43749) {
return InitializationResult.apiChecksumMismatch
}
if (uniffi_viaduct_checksum_func_allow_android_emulator_loopback() != 27278) {
return InitializationResult.apiChecksumMismatch
}
if (uniffi_viaduct_checksum_func_set_global_default_user_agent() != 48124) {
return InitializationResult.apiChecksumMismatch
}
if (uniffi_viaduct_checksum_method_backend_send_request() != 3462) {
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