feat: add crate swift-rs

This commit is contained in:
2023-12-09 10:17:41 +08:00
parent d36c06f461
commit 215b75d6da
35 changed files with 2489 additions and 2 deletions

View File

@@ -0,0 +1,31 @@
// swift-tools-version:5.3
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "test-swift",
platforms: [
.macOS(.v11),
],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "test-swift",
type: .static,
targets: ["test-swift"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
.package(name: "SwiftRs", path: "../../")
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: "test-swift",
dependencies: [.product(name: "SwiftRs", package: "SwiftRs")],
path: ".",
exclude: ["test_example.rs", "test_bindings.rs"])
]
)

View File

@@ -0,0 +1,70 @@
import Foundation
import SwiftRs
// SRArray
//
// Notice that IntArray and ArrayStruct are almost identical!
// The only actual difference between these types is how they're used in Rust,
// but if you added more fields to ArrayStruct then that wouldn't be the case anymore.
class IntArray: NSObject {
var data: SRArray<Int>
init(data: [Int]) {
self.data = SRArray(data)
}
}
@_cdecl("get_int_array")
func getIntArray() -> IntArray {
return IntArray(data: [1, 2, 3])
}
class ArrayStruct: NSObject {
var array: SRArray<Int>
init(array: [Int]) {
self.array = SRArray(array)
}
}
@_cdecl("get_array_struct")
func getArrayStruct() -> ArrayStruct {
return ArrayStruct(array: [4, 5, 6])
}
// SRObject
class CustomObject: NSObject {
var a: Int
var b: Bool
init(a: Int, b: Bool) {
self.a = a
self.b = b
}
}
@_cdecl("get_custom_object")
func getCustomObject() -> CustomObject {
return CustomObject(a: 3, b: true)
}
// SRString
@_cdecl("get_greeting")
func getGreeting(name: SRString) -> SRString {
return SRString("Hello \(name.toString())!")
}
@_cdecl("echo")
func echo(string: SRString) -> SRString {
return string
}
// SRData
@_cdecl("get_data")
func getData() -> SRData {
return SRData([1, 2, 3])
}

View File

@@ -0,0 +1,29 @@
import SwiftRs
import Foundation
class Complex: NSObject {
var a: SRString
var b: Int
var c: Bool
public init(a: SRString, b: Int, c: Bool) {
self.a = a
self.b = b
self.c = c
}
}
@_cdecl("complex_data")
func complexData() -> SRObjectArray {
return SRObjectArray([
Complex(a: SRString("Brendan"), b: 0, c: true),
Complex(a: SRString("Amod"), b: 1, c: false),
Complex(a: SRString("Lucas"), b: 2, c: true),
Complex(a: SRString("Oscar"), b: 3, c: false),
])
}
@_cdecl("echo_data")
func echoData(data: SRData) -> SRData {
return SRData(data.toArray())
}