Skip to content

Commit

Permalink
Merge pull request mozilla#2084 from Sajjon/swift_free_up_name_error_…
Browse files Browse the repository at this point in the history
…for_errors

Swift: Enable error types to be named `Error`
  • Loading branch information
bendk authored Apr 29, 2024
2 parents 0f95c82 + 9d48823 commit 3d93e26
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 13 deletions.
6 changes: 3 additions & 3 deletions fixtures/error-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,14 @@ fn return_proc_error(e: String) -> Arc<ProcErrorInterface> {

// Enums have good coverage elsewhere, but simple coverage here is good.
#[derive(thiserror::Error, uniffi::Error, Debug)]
pub enum EnumError {
pub enum Error {
#[error("Oops")]
Oops,
}

#[uniffi::export]
fn oops_enum() -> Result<(), EnumError> {
Err(EnumError::Oops)
fn oops_enum() -> Result<(), Error> {
Err(Error::Oops)
}

uniffi::include_scaffolding!("error_types");
2 changes: 2 additions & 0 deletions fixtures/error-types/tests/bindings/test.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ do {
let e = getError(message: "the error")
assert(String(describing: e) == "the error")
assert(String(reflecting: e) == "ErrorInterface { e: the error }")
assert(Error.self is Swift.Error.Type)
assert(Error.self != Swift.Error.self)
2 changes: 1 addition & 1 deletion uniffi_bindgen/src/bindings/swift/templates/Async.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fileprivate func uniffiRustCallAsync<F, T>(
completeFunc: (UInt64, UnsafeMutablePointer<RustCallStatus>) -> F,
freeFunc: (UInt64) -> (),
liftFunc: (F) throws -> T,
errorHandler: ((RustBuffer) throws -> Error)?
errorHandler: ((RustBuffer) throws -> Swift.Error)?
) async throws -> T {
// Make sure to call uniffiEnsureInitialized() since future creation doesn't have a
// RustCallStatus param, so doesn't use makeRustCall()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,4 @@ public struct {{ ffi_converter_name }}: FfiConverterRustBuffer {
{% if !contains_object_references %}
extension {{ type_name }}: Equatable, Hashable {}
{% endif %}
extension {{ type_name }}: Error { }
extension {{ type_name }}: Swift.Error { }
15 changes: 8 additions & 7 deletions uniffi_bindgen/src/bindings/swift/templates/Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,19 @@ fileprivate extension RustCallStatus {
}

private func rustCall<T>(_ callback: (UnsafeMutablePointer<RustCallStatus>) -> T) throws -> T {
try makeRustCall(callback, errorHandler: nil)
let neverThrow: ((RustBuffer) throws -> Never)? = nil
return try makeRustCall(callback, errorHandler: neverThrow)
}

private func rustCallWithError<T>(
_ errorHandler: @escaping (RustBuffer) throws -> Error,
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>(
private func makeRustCall<T, E: Swift.Error>(
_ callback: (UnsafeMutablePointer<RustCallStatus>) -> T,
errorHandler: ((RustBuffer) throws -> Error)?
errorHandler: ((RustBuffer) throws -> E)?
) throws -> T {
uniffiEnsureInitialized()
var callStatus = RustCallStatus.init()
Expand All @@ -73,9 +74,9 @@ private func makeRustCall<T>(
return returnedVal
}

private func uniffiCheckCallStatus(
private func uniffiCheckCallStatus<E: Swift.Error>(
callStatus: RustCallStatus,
errorHandler: ((RustBuffer) throws -> Error)?
errorHandler: ((RustBuffer) throws -> E)?
) throws {
switch callStatus.code {
case CALL_SUCCESS:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ open class {{ impl_class_name }}:
{%- endmatch %}
{%- endfor %}
{%- if is_error %}
Error,
Swift.Error,
{% endif %}
{{ protocol_name }} {
fileprivate let pointer: UnsafeMutableRawPointer!
Expand Down

0 comments on commit 3d93e26

Please sign in to comment.