├── .gitignore ├── Cargo.lock ├── Cargo.toml ├── Package.swift ├── README.md ├── Sources └── RustToSwift │ └── MathWizWrapper.swift ├── Tests └── RustToSwiftTests │ └── MathWizTests.swift ├── include ├── mathwiz.h └── module.modulemap └── src └── lib.rs /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | *.a 3 | *.xcframework 4 | *.zip 5 | .swiftpm 6 | .build -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "rust-to-swift" 7 | version = "0.1.0" 8 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "rust-to-swift" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | [lib] 7 | name = "mathwiz" 8 | crate-type = [ "staticlib" ] 9 | 10 | [dependencies] 11 | -------------------------------------------------------------------------------- /Package.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version:5.3 2 | // The swift-tools-version declares the minimum version of Swift required to build this package. 3 | 4 | import PackageDescription 5 | import Foundation 6 | 7 | let package = Package( 8 | name: "RustToSwift", 9 | platforms: [ 10 | .iOS(.v13), 11 | .macOS(.v11) 12 | ], 13 | products: [ 14 | .library( 15 | name: "RustToSwift", 16 | targets: ["RustToSwift"]), 17 | ], 18 | targets: [ 19 | .target( 20 | name: "RustToSwift", 21 | dependencies: ["MathWiz"]), 22 | .binaryTarget( 23 | name: "MathWiz", 24 | url: "https://github.com/tmarkovski/rust-to-swift/releases/download/1.0.0/bundle.zip", 25 | checksum: "ea7ad198b0ce05da56d8c97f36416635bb3b5c9a74b6fd65c9753e43b62c35b2"), 26 | .testTarget( 27 | name: "RustToSwiftTests", 28 | dependencies: ["RustToSwift"]), 29 | ] 30 | ) 31 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Rust to Swift 2 | 3 | This repo contains code samples for the article available at https://medium.com/@tmarkovski/from-rust-to-swift-df9bde59b7cd 4 | -------------------------------------------------------------------------------- /Sources/RustToSwift/MathWizWrapper.swift: -------------------------------------------------------------------------------- 1 | // 2 | // File.swift 3 | // 4 | // 5 | // Created by Tomislav Markovski on 1/15/22. 6 | // 7 | 8 | import Foundation 9 | import MathWiz 10 | 11 | public class MathWizWrapper { 12 | public static func Add(a: Int32, b: Int32) -> Int32 { 13 | MathWiz.add(a, b) 14 | } 15 | 16 | public static func Flip(a: Bool) -> Bool { 17 | MathWiz.flip(a) 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Tests/RustToSwiftTests/MathWizTests.swift: -------------------------------------------------------------------------------- 1 | // 2 | // File.swift 3 | // 4 | // 5 | // Created by Tomislav Markovski on 1/15/22. 6 | // 7 | 8 | import Foundation 9 | import XCTest 10 | 11 | @testable import RustToSwift 12 | 13 | final class MathWizTests: XCTestCase { 14 | func testAdd() throws { 15 | let actual = MathWizWrapper.Add(a: 1, b: 2) 16 | 17 | XCTAssertEqual(3, actual) 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /include/mathwiz.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int32_t add(int32_t a, int32_t b); 7 | 8 | bool flip(bool a); 9 | -------------------------------------------------------------------------------- /include/module.modulemap: -------------------------------------------------------------------------------- 1 | module MathWiz { 2 | header "mathwiz.h" 3 | export * 4 | } -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | #[no_mangle] 2 | pub extern "C" fn add(a: i32, b: i32) -> i32 { 3 | a + b 4 | } 5 | 6 | #[no_mangle] 7 | pub extern "C" fn flip(a: bool) -> bool { 8 | !a 9 | } 10 | --------------------------------------------------------------------------------