-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support derive(Debug) on enums (#194)
Related issue: #190 Example: ```rs // Rust #[swift_bridge::bridge] mod ffi { #[derive(Debug)] enum DeriveDebugEnum { Variant, } } ``` ```swift // Swift let debugString = String(reflecting: DeriveDebugEnum.Variant) XCTAssertEqual(debugString, "Variant") ```
- Loading branch information
Showing
18 changed files
with
307 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
crates/swift-bridge-ir/src/codegen/codegen_tests/derive_attribute_codegen_tests.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use super::{CodegenTest, ExpectedCHeader, ExpectedRustTokens, ExpectedSwiftCode}; | ||
use proc_macro2::TokenStream; | ||
use quote::quote; | ||
|
||
/// Verify that we generate debugDescription in Swift and Debug function in Rust when using #\[derive(Debug)] | ||
mod derive_debug_enum { | ||
use super::*; | ||
|
||
fn bridge_module_tokens() -> TokenStream { | ||
quote! { | ||
#[swift_bridge::bridge] | ||
mod ffi { | ||
#[derive(Debug)] | ||
enum SomeEnum { | ||
Variant1 | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn expected_rust_tokens() -> ExpectedRustTokens { | ||
ExpectedRustTokens::ContainsMany(vec![ | ||
quote! { | ||
#[derive(Copy, Clone, ::std::fmt::Debug)] | ||
pub enum SomeEnum { | ||
Variant1 | ||
} | ||
}, | ||
quote! { | ||
#[export_name = "__swift_bridge__$SomeEnum$Debug"] | ||
pub extern "C" fn __swift_bridge__SomeEnum_Debug(this: __swift_bridge__SomeEnum) -> *mut swift_bridge::string::RustString { | ||
swift_bridge::string::RustString(format!("{:?}", this.into_rust_repr())).box_into_raw() | ||
} | ||
}, | ||
]) | ||
} | ||
|
||
fn expected_swift_code() -> ExpectedSwiftCode { | ||
ExpectedSwiftCode::ContainsAfterTrim( | ||
r#" | ||
extension SomeEnum: CustomDebugStringConvertible { | ||
public var debugDescription: String { | ||
RustString(ptr: __swift_bridge__$SomeEnum$Debug(self.intoFfiRepr())).toString() | ||
} | ||
} | ||
"#, | ||
) | ||
} | ||
|
||
fn expected_c_header() -> ExpectedCHeader { | ||
ExpectedCHeader::ContainsAfterTrim( | ||
r#" | ||
void* __swift_bridge__$SomeEnum$Debug(__swift_bridge__$SomeEnum this); | ||
"#, | ||
) | ||
} | ||
|
||
#[test] | ||
fn generates_enum_to_and_from_ffi_conversions_no_data() { | ||
CodegenTest { | ||
bridge_module: bridge_module_tokens().into(), | ||
expected_rust_tokens: expected_rust_tokens(), | ||
expected_swift_code: expected_swift_code(), | ||
expected_c_header: expected_c_header(), | ||
} | ||
.test(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.