Skip to content

Commit

Permalink
Merge pull request #44 from xmtp/np/optimistic-sending
Browse files Browse the repository at this point in the history
Optimistic Sending Bindings
  • Loading branch information
nplasterer authored Jul 8, 2024
2 parents 6bcf6f2 + f865b08 commit b19830d
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 6 deletions.
4 changes: 2 additions & 2 deletions LibXMTP.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'LibXMTP'
s.version = '0.5.4-beta2'
s.version = '0.5.4-beta3'
s.summary = 'XMTP shared Rust code that powers cross-platform SDKs'

s.homepage = 'https://github.com/xmtp/libxmtp-swift'
Expand All @@ -10,7 +10,7 @@ Pod::Spec.new do |s|
s.platform = :ios, '14.0', :macos, '11.0'
s.swift_version = '5.3'

s.source = { :http => "https://github.com/xmtp/libxmtp/releases/download/swift-bindings-5c78fba/LibXMTPSwiftFFI.zip", :type => :zip }
s.source = { :http => "https://github.com/xmtp/libxmtp/releases/download/swift-bindings-d4d8134/LibXMTPSwiftFFI.zip", :type => :zip }
s.vendored_frameworks = 'LibXMTPSwiftFFI.xcframework'
s.source_files = 'Sources/LibXMTP/**/*'
end
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ let package = Package(
),
.binaryTarget(
name: "LibXMTPSwiftFFI",
url: "https://github.com/xmtp/libxmtp/releases/download/swift-bindings-5c78fba/LibXMTPSwiftFFI.zip",
checksum: "eb967303095d389701f957c5fd9f2e72eafddd5ae71bdd249a6dab26dfc30fb9"
url: "https://github.com/xmtp/libxmtp/releases/download/swift-bindings-d4d8134/LibXMTPSwiftFFI.zip",
checksum: "26b52514c78edc030c73d7a9b21d7b55c2d42342dce4c7fb5480f530ab3cca30"
),
.testTarget(name: "LibXMTPTests", dependencies: ["LibXMTP"]),
]
Expand Down
4 changes: 2 additions & 2 deletions Sources/LibXMTP/libxmtp-version.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Version: 5c78fbad
Version: d4d8134a
Branch: main
Date: 2024-07-03 01:23:38 +0000
Date: 2024-07-08 15:44:32 +0000
131 changes: 131 additions & 0 deletions Sources/LibXMTP/xmtpv3.swift
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,11 @@ public protocol FfiGroupProtocol: AnyObject {

func send(contentBytes: Data) async throws -> Data

/**
* send a message without immediately publishing to the delivery service.
*/
func sendOptimistic(contentBytes: Data) throws -> FfiUnpublishedMessage

func stream(messageCallback: FfiMessageCallback) async throws -> FfiStreamCloser

func superAdminList() throws -> [String]
Expand Down Expand Up @@ -1071,6 +1076,16 @@ open class FfiGroup:
)
}

/**
* send a message without immediately publishing to the delivery service.
*/
open func sendOptimistic(contentBytes: Data) throws -> FfiUnpublishedMessage {
return try FfiConverterTypeFfiUnpublishedMessage.lift(rustCallWithError(FfiConverterTypeGenericError.lift) {
uniffi_xmtpv3_fn_method_ffigroup_send_optimistic(self.uniffiClonePointer(),
FfiConverterData.lower(contentBytes), $0)
})
}

open func stream(messageCallback: FfiMessageCallback) async throws -> FfiStreamCloser {
return
try await uniffiRustCallAsync(
Expand Down Expand Up @@ -1703,6 +1718,113 @@ public func FfiConverterTypeFfiStreamCloser_lower(_ value: FfiStreamCloser) -> U
return FfiConverterTypeFfiStreamCloser.lower(value)
}

public protocol FfiUnpublishedMessageProtocol: AnyObject {
func id() -> Data

func publish() async throws
}

open class FfiUnpublishedMessage:
FfiUnpublishedMessageProtocol
{
fileprivate let pointer: UnsafeMutableRawPointer!

/// Used to instantiate a [FFIObject] without an actual pointer, for fakes in tests, mostly.
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`.
public required 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.
public init(noPointer _: NoPointer) {
pointer = nil
}

public func uniffiClonePointer() -> UnsafeMutableRawPointer {
return try! rustCall { uniffi_xmtpv3_fn_clone_ffiunpublishedmessage(self.pointer, $0) }
}

// No primary constructor declared for this class.

deinit {
guard let pointer = pointer else {
return
}

try! rustCall { uniffi_xmtpv3_fn_free_ffiunpublishedmessage(pointer, $0) }
}

open func id() -> Data {
return try! FfiConverterData.lift(try! rustCall {
uniffi_xmtpv3_fn_method_ffiunpublishedmessage_id(self.uniffiClonePointer(), $0)
})
}

open func publish() async throws {
return
try await uniffiRustCallAsync(
rustFutureFunc: {
uniffi_xmtpv3_fn_method_ffiunpublishedmessage_publish(
self.uniffiClonePointer()
)
},
pollFunc: ffi_xmtpv3_rust_future_poll_void,
completeFunc: ffi_xmtpv3_rust_future_complete_void,
freeFunc: ffi_xmtpv3_rust_future_free_void,
liftFunc: { $0 },
errorHandler: FfiConverterTypeGenericError.lift
)
}
}

public struct FfiConverterTypeFfiUnpublishedMessage: FfiConverter {
typealias FfiType = UnsafeMutableRawPointer
typealias SwiftType = FfiUnpublishedMessage

public static func lift(_ pointer: UnsafeMutableRawPointer) throws -> FfiUnpublishedMessage {
return FfiUnpublishedMessage(unsafeFromRawPointer: pointer)
}

public static func lower(_ value: FfiUnpublishedMessage) -> UnsafeMutableRawPointer {
return value.uniffiClonePointer()
}

public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> FfiUnpublishedMessage {
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: FfiUnpublishedMessage, 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)))))
}
}

public func FfiConverterTypeFfiUnpublishedMessage_lift(_ pointer: UnsafeMutableRawPointer) throws -> FfiUnpublishedMessage {
return try FfiConverterTypeFfiUnpublishedMessage.lift(pointer)
}

public func FfiConverterTypeFfiUnpublishedMessage_lower(_ value: FfiUnpublishedMessage) -> UnsafeMutableRawPointer {
return FfiConverterTypeFfiUnpublishedMessage.lower(value)
}

public protocol FfiV2ApiClientProtocol: AnyObject {
func batchQuery(req: FfiV2BatchQueryRequest) async throws -> FfiV2BatchQueryResponse

Expand Down Expand Up @@ -4913,6 +5035,9 @@ private var initializationResult: InitializationResult {
if uniffi_xmtpv3_checksum_method_ffigroup_send() != 37701 {
return InitializationResult.apiChecksumMismatch
}
if uniffi_xmtpv3_checksum_method_ffigroup_send_optimistic() != 22919 {
return InitializationResult.apiChecksumMismatch
}
if uniffi_xmtpv3_checksum_method_ffigroup_stream() != 45558 {
return InitializationResult.apiChecksumMismatch
}
Expand Down Expand Up @@ -4970,6 +5095,12 @@ private var initializationResult: InitializationResult {
if uniffi_xmtpv3_checksum_method_ffistreamcloser_is_closed() != 62423 {
return InitializationResult.apiChecksumMismatch
}
if uniffi_xmtpv3_checksum_method_ffiunpublishedmessage_id() != 4148 {
return InitializationResult.apiChecksumMismatch
}
if uniffi_xmtpv3_checksum_method_ffiunpublishedmessage_publish() != 47708 {
return InitializationResult.apiChecksumMismatch
}
if uniffi_xmtpv3_checksum_method_ffiv2apiclient_batch_query() != 26551 {
return InitializationResult.apiChecksumMismatch
}
Expand Down

0 comments on commit b19830d

Please sign in to comment.