diff --git a/SwiftRustIntegrationTestRunner/SwiftRustIntegrationTestRunner/RustFnUsesOpaqueSwiftType.swift b/SwiftRustIntegrationTestRunner/SwiftRustIntegrationTestRunner/RustFnUsesOpaqueSwiftType.swift index 565567b3..1c4138fe 100644 --- a/SwiftRustIntegrationTestRunner/SwiftRustIntegrationTestRunner/RustFnUsesOpaqueSwiftType.swift +++ b/SwiftRustIntegrationTestRunner/SwiftRustIntegrationTestRunner/RustFnUsesOpaqueSwiftType.swift @@ -8,7 +8,7 @@ import Foundation /// We expose this to the `rust_function_return_swift_type.rs` test. -class SomeSwiftType { +public class SomeSwiftType { var text: String init() { diff --git a/book/src/SUMMARY.md b/book/src/SUMMARY.md index 0559b4fb..63999a5e 100644 --- a/book/src/SUMMARY.md +++ b/book/src/SUMMARY.md @@ -7,6 +7,7 @@ - [Building](./building/README.md) - [Xcode + Cargo](./building/xcode-and-cargo/README.md) - [swiftc + Cargo](./building/swiftc-and-cargo/README.md) + - [Swift Package](./building/swift-package/README.md) - [The Bridge Module](./bridge-module/README.md) - [extern "Rust"](./bridge-module/extern-rust/README.md) diff --git a/book/src/building/swift-package/README.md b/book/src/building/swift-package/README.md new file mode 100644 index 00000000..ec19f39a --- /dev/null +++ b/book/src/building/swift-package/README.md @@ -0,0 +1,253 @@ +# Bundling Rust code as a Swift package + +In this chapter we'll walk through bundling your Rust library into a Swift Package. + +Note that Swift Packages that contain native libraries only work on Apple hardware. + +You should avoid bundling your Rust code into a Swift Package if you plan to target Linux, Windows or any other non-Apple target. + +## Project setup + +```bash +mkdir rust-swift-package && cd rust-swift-package +``` + +### Rust project setup + +```bash +cargo new my-rust-lib --lib +cd my_rust_lib +``` + +```toml +# my-rust-lib/Cargo.toml + +[lib] +crate-type = ["staticlib"] + +[build-dependencies] +swift-bridge-build = "0.1" + +[dependencies] +swift-bridge = "0.1" +``` + +In `src/lib.rs`, add the following: + +```rust +// my-rust-lib/src/lib.rs +#[swift_bridge::bridge] +mod ffi { + extern "Rust" { + fn hello_rust() -> String; + } +} + +fn hello_rust() -> String { + String::from("Hello Rust!") +} +``` + +Add a new `build.rs` file (`touch build.rs`): +```rust +// my-rust-lib/build.rs + +use std::path::PathBuf; + +fn main() { + let out_dir = PathBuf::from("./generated"); + + let bridges = vec!["src/lib.rs"]; + for path in &bridges { + println!("cargo:rerun-if-changed={}", path); + } + + swift_bridge_build::parse_bridges(bridges) + .write_all_concatenated(out_dir, env!("CARGO_PKG_NAME")); +} +``` + +Build the project for the desired platforms: + +```bash +export SWIFT_BRIDGE_OUT_DIR="$(pwd)/generated" +cargo build --target x86_64-apple-darwin +cargo build --target aarch64-apple-ios +cargo build --target x86_64-apple-ios +``` + +## Creating the XCFramework + +Go back to the root of the project and make a new directory `cd ..`. + +```bash +mkdir MyFramework && cd $_ +``` + +Copy the generated libraries and the headers to this folder: +```bash +mkdir include +touch include/module.modulemap +cp ../my-rust-lib/generated/SwiftBridgeCore.h ./include +cp ../my-rust-lib/generated/my_rust_lib/my_rust_lib.h ./includ +mkdir ios +cp ../my-rust-lib/target/aarch64-apple-ios/debug/libmy_rust_lib.a ./ios +mkdir macos +cp ../my-rust-lib/target/x86_64-apple-darwin/debug/libmy_rust_lib.a ./macos +mkdir simulator +cp ../my-rust-lib/target/x86_64-apple-ios/debug/libmy_rust_lib.a ./simulator +``` + +This should result in the follwing folder structure: +``` +MyFramework +├── include +│ ├── SwiftBridgeCore.h +│ ├── module.modulemap +│ └── my_rust_lib.h +├── ios +│ └── libmy_rust_lib.a +├── macos +│ └── libmy_rust_lib.a +└── simulator + └── libmy_rust_lib.a +``` + +Edit `include/module.modulemap`: + +```modulemap +module MyRustLib { + header "my_rust_lib.h" + header "SwiftBridgeCore.h" + export * +} +``` + +Now it is time to build the xcframework: + +```bash +xcodebuild -create-xcframework \ + -library simulator/libmy_rust_lib.a \ + -headers include \ + -library ios/libmy_rust_lib.a \ + -headers include \ + -library macos/libmy_rust_lib.a \ + -headers include \ + -output MyRustLib.xcframework +``` + +*The order of the `library` tags is important, but we don't currently know why* + +## Creating the Swift package + +Go back to the root of the project (`cd ..`) and create a new Swift package: + +```bash +mkdir MySwiftPackage && cd MySwiftPackage +``` + +Now either do `mkdir -r Sources/MySwiftPackage` and `touch Package.swift`, or use `swift package init --type library`. + +Copy the xcframework and generated swift files to this folder: +```bash +cp -r ../MyFramework/MyRustLib.xcframework ./ +cp ../my-rust-lib/generated/SwiftBridgeCore.swift Sources/MySwiftPackage +cp ../my-rust-lib/generated/my_rust_lib/my_rust_lib.swift Sources/MySwiftPackage +``` + +The folder structure should be: +``` +MySwiftPackage +├── Sources +│ └── MySwiftPackage +│ └── SwiftBridgeCore.swift +│ └── my_rust_lib.swift +├── MyRustLib.b.xcframework +└── Package.swift +``` + +Add the framework as a binary target to `Package.swift`: +```swift +// MySwiftPackage/Package.swift + +// swift-tools-version:5.5.0 +import PackageDescription +let package = Package( + name: "MySwiftPackage", + products: [ + .library( + name: "MySwiftPackage", + targets: ["MySwiftPackage"]), + ], + dependencies: [ + + ], + targets: [ + .binaryTarget( + name: "MyRustLib", + path: "MyRustLib.xcframework" + ), + .target( + name: "MySwiftPackge", + dependencies: ["MyRustLib"]), + ] +) +``` + + +We will need to import our rust library in `my_rust_lib.swift` and `SwiftBridgeCore.swift`: + +```swift +// MySwiftPackage/Sources/MySwiftPackage/SwiftBridgeCore.swift +import MyRustLib +``` + +```swift +// MySwiftPackage/Sources/MySwiftPackage/my_rust_lib.swift +import MyRustLib +``` + +## Using the Swift Package + +We now have a Swift Package which we can include in other projects using the Swift Package Manager. + +### Example: MacOS executable +Here is an example of an executable project located in `rust-swift-project/testPackage`. + +```swift +// testPackage/Package.swift + +// swift-tools-version:5.5.0 +import PackageDescription +let package = Package( + name: "testPackage", + dependencies: [ + .package(path: "../MySwiftPackage") + ], + targets: [ + .executableTarget( + name: "testPackage", + dependencies: [ + .product(name: "MySwiftPackage", package: "MySwiftPackage") + ]) + ] +) +``` + +```swift +// testPackage/Sources/testPackage/main.swift +import MySwiftPackage + +print(hello_rust().toString()) +``` + +``` +$ swift run +Hello Rust! +``` + +### Example: iOS app + +To add the package to an iOS app in XCode, go to the target's general panel, click the `+` button in the `Frameworks, Libraries, and Embedded Content` section. Then, click `Add Other` and choose `Add Package Dependency`. + +Import and use it in the same way as the executable. diff --git a/build.rs b/build.rs index 5bf4ad58..67e8d67a 100644 --- a/build.rs +++ b/build.rs @@ -143,19 +143,19 @@ fn conform_to_vectorizable(swift_ty: &str, rust_ty: &str) -> String { format!( r#" extension {swift_ty}: Vectorizable {{ - static func vecOfSelfNew() -> UnsafeMutableRawPointer {{ + public static func vecOfSelfNew() -> UnsafeMutableRawPointer {{ __swift_bridge__$Vec_{rust_ty}$new() }} - static func vecOfSelfFree(vecPtr: UnsafeMutableRawPointer) {{ + public static func vecOfSelfFree(vecPtr: UnsafeMutableRawPointer) {{ __swift_bridge__$Vec_{rust_ty}$_free(vecPtr) }} - static func vecOfSelfPush(vecPtr: UnsafeMutableRawPointer, value: Self) {{ + public static func vecOfSelfPush(vecPtr: UnsafeMutableRawPointer, value: Self) {{ __swift_bridge__$Vec_{rust_ty}$push(vecPtr, value) }} - static func vecOfSelfPop(vecPtr: UnsafeMutableRawPointer) -> Optional {{ + public static func vecOfSelfPop(vecPtr: UnsafeMutableRawPointer) -> Optional {{ let val = __swift_bridge__$Vec_{rust_ty}$pop(vecPtr) if val.is_some {{ return val.val @@ -164,7 +164,7 @@ extension {swift_ty}: Vectorizable {{ }} }} - static func vecOfSelfGet(vecPtr: UnsafeMutableRawPointer, index: UInt) -> Optional {{ + public static func vecOfSelfGet(vecPtr: UnsafeMutableRawPointer, index: UInt) -> Optional {{ let val = __swift_bridge__$Vec_{rust_ty}$get(vecPtr, index) if val.is_some {{ return val.val @@ -173,7 +173,7 @@ extension {swift_ty}: Vectorizable {{ }} }} - static func vecOfSelfGetMut(vecPtr: UnsafeMutableRawPointer, index: UInt) -> Optional {{ + public static func vecOfSelfGetMut(vecPtr: UnsafeMutableRawPointer, index: UInt) -> Optional {{ let val = __swift_bridge__$Vec_{rust_ty}$get_mut(vecPtr, index) if val.is_some {{ return val.val @@ -182,7 +182,7 @@ extension {swift_ty}: Vectorizable {{ }} }} - static func vecOfSelfLen(vecPtr: UnsafeMutableRawPointer) -> UInt {{ + public static func vecOfSelfLen(vecPtr: UnsafeMutableRawPointer) -> UInt {{ __swift_bridge__$Vec_{rust_ty}$len(vecPtr) }} }} diff --git a/crates/swift-bridge-ir/src/codegen/codegen_tests/extern_rust_method_swift_class_placement_codegen_tests.rs b/crates/swift-bridge-ir/src/codegen/codegen_tests/extern_rust_method_swift_class_placement_codegen_tests.rs index 0628946b..2dbb73d5 100644 --- a/crates/swift-bridge-ir/src/codegen/codegen_tests/extern_rust_method_swift_class_placement_codegen_tests.rs +++ b/crates/swift-bridge-ir/src/codegen/codegen_tests/extern_rust_method_swift_class_placement_codegen_tests.rs @@ -50,11 +50,11 @@ public class SomeType: SomeTypeRefMut { } } extension SomeType { - func a() { + public func a() { __swift_bridge__$SomeType$a({isOwned = false; return ptr;}()) } - func b() { + public func b() { __swift_bridge__$SomeType$b({isOwned = false; return ptr;}()) } } @@ -64,11 +64,11 @@ public class SomeTypeRefMut: SomeTypeRef { } } extension SomeTypeRefMut { - func e() { + public func e() { __swift_bridge__$SomeType$e(ptr) } - func f() { + public func f() { __swift_bridge__$SomeType$f(ptr) } } @@ -80,11 +80,11 @@ public class SomeTypeRef { } } extension SomeTypeRef { - func c() { + public func c() { __swift_bridge__$SomeType$c(ptr) } - func d() { + public func d() { __swift_bridge__$SomeType$d(ptr) } } diff --git a/crates/swift-bridge-ir/src/codegen/codegen_tests/vec_codegen_tests.rs b/crates/swift-bridge-ir/src/codegen/codegen_tests/vec_codegen_tests.rs index 0da7011d..a65d51a1 100644 --- a/crates/swift-bridge-ir/src/codegen/codegen_tests/vec_codegen_tests.rs +++ b/crates/swift-bridge-ir/src/codegen/codegen_tests/vec_codegen_tests.rs @@ -93,19 +93,19 @@ mod extern_rust_type_vec_support { ExpectedSwiftCode::ContainsAfterTrim( r#" extension MyRustType: Vectorizable { - static func vecOfSelfNew() -> UnsafeMutableRawPointer { + public static func vecOfSelfNew() -> UnsafeMutableRawPointer { __swift_bridge__$Vec_MyRustType$new() } - static func vecOfSelfFree(vecPtr: UnsafeMutableRawPointer) { + public static func vecOfSelfFree(vecPtr: UnsafeMutableRawPointer) { __swift_bridge__$Vec_MyRustType$drop(vecPtr) } - static func vecOfSelfPush(vecPtr: UnsafeMutableRawPointer, value: MyRustType) { + public static func vecOfSelfPush(vecPtr: UnsafeMutableRawPointer, value: MyRustType) { __swift_bridge__$Vec_MyRustType$push(vecPtr, {value.isOwned = false; return value.ptr;}()) } - static func vecOfSelfPop(vecPtr: UnsafeMutableRawPointer) -> Optional { + public static func vecOfSelfPop(vecPtr: UnsafeMutableRawPointer) -> Optional { let pointer = __swift_bridge__$Vec_MyRustType$pop(vecPtr) if pointer == nil { return nil @@ -114,7 +114,7 @@ extension MyRustType: Vectorizable { } } - static func vecOfSelfGet(vecPtr: UnsafeMutableRawPointer, index: UInt) -> Optional { + public static func vecOfSelfGet(vecPtr: UnsafeMutableRawPointer, index: UInt) -> Optional { let pointer = __swift_bridge__$Vec_MyRustType$get(vecPtr, index) if pointer == nil { return nil @@ -123,7 +123,7 @@ extension MyRustType: Vectorizable { } } - static func vecOfSelfGetMut(vecPtr: UnsafeMutableRawPointer, index: UInt) -> Optional { + public static func vecOfSelfGetMut(vecPtr: UnsafeMutableRawPointer, index: UInt) -> Optional { let pointer = __swift_bridge__$Vec_MyRustType$get_mut(vecPtr, index) if pointer == nil { return nil @@ -132,7 +132,7 @@ extension MyRustType: Vectorizable { } } - static func vecOfSelfLen(vecPtr: UnsafeMutableRawPointer) -> UInt { + public static func vecOfSelfLen(vecPtr: UnsafeMutableRawPointer) -> UInt { __swift_bridge__$Vec_MyRustType$len(vecPtr) } } diff --git a/crates/swift-bridge-ir/src/codegen/generate_swift.rs b/crates/swift-bridge-ir/src/codegen/generate_swift.rs index c9742cf0..6a6cd336 100644 --- a/crates/swift-bridge-ir/src/codegen/generate_swift.rs +++ b/crates/swift-bridge-ir/src/codegen/generate_swift.rs @@ -406,7 +406,7 @@ fn gen_func_swift_calls_rust( let swift_class_func_name = if function.is_swift_initializer { "convenience init".to_string() } else { - format!("func {}", fn_name.as_str()) + format!("public func {}", fn_name.as_str()) }; let indentation = if function.associated_type.is_some() { @@ -734,7 +734,7 @@ mod tests { let generated = module.generate_swift(&CodegenConfig::no_features_enabled()); let expected = r#" -func foo() { +public func foo() { __swift_bridge__$foo() } "#; @@ -826,7 +826,7 @@ func __swift_bridge__MyType_foo (_ this: UnsafeMutableRawPointer) -> __private__ let generated = module.generate_swift(&CodegenConfig::no_features_enabled()); let expected = r#" -func foo(_ bar: UInt8) { +public func foo(_ bar: UInt8) { __swift_bridge__$foo(bar) } "#; @@ -849,7 +849,7 @@ func foo(_ bar: UInt8) { let generated = module.generate_swift(&CodegenConfig::no_features_enabled()); let expected = r#" -func foo() -> UInt32 { +public func foo() -> UInt32 { __swift_bridge__$foo() } "#; @@ -906,7 +906,7 @@ func __swift_bridge__Foo__free (ptr: UnsafeMutableRawPointer) { /// Verify that we generated a function that Rust can use to reduce a Swift class instance's /// reference count. #[test] - fn extern_swift_claas_init() { + fn extern_swift_class_init() { let tokens = quote! { mod foo { extern "Swift" { @@ -1066,7 +1066,7 @@ public class FooRef { } } extension FooRef { - func bar() -> UInt8 { + public func bar() -> UInt8 { __swift_bridge__$Foo$bar(ptr) } } @@ -1099,7 +1099,7 @@ public class FooRef { } } extension FooRef { - func bar(_ other: FooRef) { + public func bar(_ other: FooRef) { __swift_bridge__$Foo$bar(ptr, other.ptr) } } @@ -1133,7 +1133,7 @@ public class FooRef { } } extension FooRef { - class func bar() { + class public func bar() { __swift_bridge__$Foo$bar() } } @@ -1183,7 +1183,7 @@ func __swift_bridge__Foo_bar (_ arg: UInt8) { let generated = module.generate_swift(&CodegenConfig::no_features_enabled()); let expected = r#" -func foo() -> RustString { +public func foo() -> RustString { RustString(ptr: __swift_bridge__$foo()) } "#; diff --git a/crates/swift-bridge-ir/src/codegen/generate_swift/vec.rs b/crates/swift-bridge-ir/src/codegen/generate_swift/vec.rs index 3b72a058..6434c4d7 100644 --- a/crates/swift-bridge-ir/src/codegen/generate_swift/vec.rs +++ b/crates/swift-bridge-ir/src/codegen/generate_swift/vec.rs @@ -4,19 +4,19 @@ use proc_macro2::Ident; pub(super) fn generate_vectorizable_extension(ty: &Ident) -> String { format!( r#"extension {ty}: Vectorizable {{ - static func vecOfSelfNew() -> UnsafeMutableRawPointer {{ + public static func vecOfSelfNew() -> UnsafeMutableRawPointer {{ __swift_bridge__$Vec_{ty}$new() }} - static func vecOfSelfFree(vecPtr: UnsafeMutableRawPointer) {{ + public static func vecOfSelfFree(vecPtr: UnsafeMutableRawPointer) {{ __swift_bridge__$Vec_{ty}$drop(vecPtr) }} - static func vecOfSelfPush(vecPtr: UnsafeMutableRawPointer, value: {ty}) {{ + public static func vecOfSelfPush(vecPtr: UnsafeMutableRawPointer, value: {ty}) {{ __swift_bridge__$Vec_{ty}$push(vecPtr, {{value.isOwned = false; return value.ptr;}}()) }} - static func vecOfSelfPop(vecPtr: UnsafeMutableRawPointer) -> Optional {{ + public static func vecOfSelfPop(vecPtr: UnsafeMutableRawPointer) -> Optional {{ let pointer = __swift_bridge__$Vec_{ty}$pop(vecPtr) if pointer == nil {{ return nil @@ -25,7 +25,7 @@ pub(super) fn generate_vectorizable_extension(ty: &Ident) -> String { }} }} - static func vecOfSelfGet(vecPtr: UnsafeMutableRawPointer, index: UInt) -> Optional<{ty}Ref> {{ + public static func vecOfSelfGet(vecPtr: UnsafeMutableRawPointer, index: UInt) -> Optional<{ty}Ref> {{ let pointer = __swift_bridge__$Vec_{ty}$get(vecPtr, index) if pointer == nil {{ return nil @@ -34,7 +34,7 @@ pub(super) fn generate_vectorizable_extension(ty: &Ident) -> String { }} }} - static func vecOfSelfGetMut(vecPtr: UnsafeMutableRawPointer, index: UInt) -> Optional<{ty}RefMut> {{ + public static func vecOfSelfGetMut(vecPtr: UnsafeMutableRawPointer, index: UInt) -> Optional<{ty}RefMut> {{ let pointer = __swift_bridge__$Vec_{ty}$get_mut(vecPtr, index) if pointer == nil {{ return nil @@ -43,7 +43,7 @@ pub(super) fn generate_vectorizable_extension(ty: &Ident) -> String { }} }} - static func vecOfSelfLen(vecPtr: UnsafeMutableRawPointer) -> UInt {{ + public static func vecOfSelfLen(vecPtr: UnsafeMutableRawPointer) -> UInt {{ __swift_bridge__$Vec_{ty}$len(vecPtr) }} }} @@ -64,19 +64,19 @@ mod tests { fn generates_vectorizable_extension() { let expected = r#" extension ARustType: Vectorizable { - static func vecOfSelfNew() -> UnsafeMutableRawPointer { + public static func vecOfSelfNew() -> UnsafeMutableRawPointer { __swift_bridge__$Vec_ARustType$new() } - static func vecOfSelfFree(vecPtr: UnsafeMutableRawPointer) { + public static func vecOfSelfFree(vecPtr: UnsafeMutableRawPointer) { __swift_bridge__$Vec_ARustType$drop(vecPtr) } - static func vecOfSelfPush(vecPtr: UnsafeMutableRawPointer, value: ARustType) { + public static func vecOfSelfPush(vecPtr: UnsafeMutableRawPointer, value: ARustType) { __swift_bridge__$Vec_ARustType$push(vecPtr, {value.isOwned = false; return value.ptr;}()) } - static func vecOfSelfPop(vecPtr: UnsafeMutableRawPointer) -> Optional { + public static func vecOfSelfPop(vecPtr: UnsafeMutableRawPointer) -> Optional { let pointer = __swift_bridge__$Vec_ARustType$pop(vecPtr) if pointer == nil { return nil @@ -85,7 +85,7 @@ extension ARustType: Vectorizable { } } - static func vecOfSelfGet(vecPtr: UnsafeMutableRawPointer, index: UInt) -> Optional { + public static func vecOfSelfGet(vecPtr: UnsafeMutableRawPointer, index: UInt) -> Optional { let pointer = __swift_bridge__$Vec_ARustType$get(vecPtr, index) if pointer == nil { return nil @@ -94,7 +94,7 @@ extension ARustType: Vectorizable { } } - static func vecOfSelfGetMut(vecPtr: UnsafeMutableRawPointer, index: UInt) -> Optional { + public static func vecOfSelfGetMut(vecPtr: UnsafeMutableRawPointer, index: UInt) -> Optional { let pointer = __swift_bridge__$Vec_ARustType$get_mut(vecPtr, index) if pointer == nil { return nil @@ -103,7 +103,7 @@ extension ARustType: Vectorizable { } } - static func vecOfSelfLen(vecPtr: UnsafeMutableRawPointer) -> UInt { + public static func vecOfSelfLen(vecPtr: UnsafeMutableRawPointer) -> UInt { __swift_bridge__$Vec_ARustType$len(vecPtr) } } diff --git a/src/std_bridge/rust_vec.swift b/src/std_bridge/rust_vec.swift index 7086189e..a81a9c83 100644 --- a/src/std_bridge/rust_vec.swift +++ b/src/std_bridge/rust_vec.swift @@ -1,7 +1,7 @@ // TODO: // Implement iterator https://developer.apple.com/documentation/swift/iteratorprotocol -class RustVec { +public class RustVec { var ptr: UnsafeMutableRawPointer var isOwned: Bool = true @@ -40,12 +40,12 @@ class RustVec { } extension RustVec: Sequence { - func makeIterator() -> RustVecIterator { + public func makeIterator() -> RustVecIterator { return RustVecIterator(self) } } -struct RustVecIterator: IteratorProtocol { +public struct RustVecIterator: IteratorProtocol { var rustVec: RustVec var index: UInt = 0 @@ -53,7 +53,7 @@ struct RustVecIterator: IteratorProtocol { self.rustVec = rustVec } - mutating func next() -> T.SelfRef? { + public mutating func next() -> T.SelfRef? { let val = rustVec.get(index: index) index += 1 return val @@ -61,21 +61,21 @@ struct RustVecIterator: IteratorProtocol { } extension RustVec: Collection { - typealias Index = Int + public typealias Index = Int - func index(after i: Int) -> Int { + public func index(after i: Int) -> Int { i + 1 } - subscript(position: Int) -> T.SelfRef { + public subscript(position: Int) -> T.SelfRef { self.get(index: UInt(position))! } - var startIndex: Int { + public var startIndex: Int { 0 } - var endIndex: Int { + public var endIndex: Int { self.len() } } @@ -105,7 +105,7 @@ extension Array { } } -protocol Vectorizable { +public protocol Vectorizable { associatedtype SelfRef associatedtype SelfRefMut diff --git a/src/std_bridge/string.swift b/src/std_bridge/string.swift index c8630960..6fe1dadd 100644 --- a/src/std_bridge/string.swift +++ b/src/std_bridge/string.swift @@ -25,7 +25,7 @@ class SwiftString { } extension RustString { - func toString() -> String { + public func toString() -> String { let str = self.as_str() let string = str.toString() @@ -39,7 +39,7 @@ extension RustStr { return bytes } - func toString() -> String { + public func toString() -> String { let bytes = self.toBufferPointer() return String(bytes: bytes, encoding: .utf8)! } @@ -58,16 +58,16 @@ extension RustStr: Equatable { } } -protocol IntoRustString { +public protocol IntoRustString { func intoRustString() -> RustString; } -protocol ToRustStr { +public protocol ToRustStr { func toRustStr (_ withUnsafeRustStr: (RustStr) -> T) -> T; } extension String: IntoRustString { - func intoRustString() -> RustString { + public func intoRustString() -> RustString { // TODO: When passing an owned Swift std String to Rust we've being wasteful here in that // we're creating a RustString (which involves Boxing a Rust std::string::String) // only to unbox it back into a String once it gets to the Rust side. @@ -79,7 +79,7 @@ extension String: IntoRustString { } extension RustString: IntoRustString { - func intoRustString() -> RustString { + public func intoRustString() -> RustString { self } } @@ -102,7 +102,7 @@ func optionalStringIntoRustString(_ string: Optional) -> R extension String: ToRustStr { /// Safely get a scoped pointer to the String and then call the callback with a RustStr /// that uses that pointer. - func toRustStr (_ withUnsafeRustStr: (RustStr) -> T) -> T { + public func toRustStr (_ withUnsafeRustStr: (RustStr) -> T) -> T { return self.utf8CString.withUnsafeBufferPointer({ bufferPtr in let rustStr = RustStr( start: UnsafeMutableRawPointer(mutating: bufferPtr.baseAddress!).assumingMemoryBound(to: UInt8.self), @@ -115,7 +115,7 @@ extension String: ToRustStr { } extension RustStr: ToRustStr { - func toRustStr (_ withUnsafeRustStr: (RustStr) -> T) -> T { + public func toRustStr (_ withUnsafeRustStr: (RustStr) -> T) -> T { return withUnsafeRustStr(self) } } @@ -126,4 +126,4 @@ func optionalRustStrToRustStr(_ str: Optional, _ withUnsafeR } else { return withUnsafeRustStr(RustStr(start: nil, len: 0)) } -} \ No newline at end of file +}