Skip to content

Commit

Permalink
Working package.swift
Browse files Browse the repository at this point in the history
Signed-off-by: Joe Mattiello <[email protected]>
  • Loading branch information
JoeMatt committed Sep 16, 2024
1 parent ded2e03 commit a238687
Show file tree
Hide file tree
Showing 11 changed files with 180 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ em_proxy.h
/em_proxy.xcframework
/em_proxy.xcframework.zip
/include
xcuserdata
7 changes: 7 additions & 0 deletions .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEWorkspaceSharedSettings_AutocreateContextsIfNeeded</key>
<true/>
</dict>
</plist>
34 changes: 34 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// swift-tools-version: 6.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "em_proxy",
products: [
.library(
name: "em_proxy",
targets: ["em_proxy"]),
],
targets: [
.target(
name: "em_proxy",
dependencies: ["libem_proxy"]),

.target(
name: "libem_proxy",
dependencies: ["em_proxy-binary"]
),

.binaryTarget(
name: "em_proxy-binary",
url: "https://github.com/SideStore/em_proxy/releases/download/build/em_proxy.xcframework.zip",
checksum: "79f90075b8ff2f47540a5bccf5fb7740905cda63463f833e2505256237df3c1b"),

.testTarget(
name: "em_proxyTests",
dependencies: ["em_proxy", "libem_proxy"]
),
],
swiftLanguageModes: [.v5]
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Foundation
import libem_proxy

extension RustString {
public func toString() -> String {
Expand Down Expand Up @@ -35,10 +36,6 @@ public protocol IntoRustString {
func intoRustString() -> RustString;
}

public protocol ToRustStr {
func toRustStr<T> (_ withUnsafeRustStr: (RustStr) -> T) -> T;
}

extension String: IntoRustString {
public func intoRustString() -> RustString {
// TODO: When passing an owned Swift std String to Rust we've being wasteful here in that
Expand Down Expand Up @@ -72,6 +69,19 @@ func optionalStringIntoRustString<S: IntoRustString>(_ string: Optional<S>) -> R
}
}

/// Used to safely get a pointer to a sequence of utf8 bytes, represented as a `RustStr`.
///
/// For example, the Swift `String` implementation of the `ToRustStr` protocol does the following:
/// 1. Use Swift's `String.utf8.withUnsafeBufferPointer` to get a pointer to the strings underlying
/// utf8 bytes.
/// 2. Construct a `RustStr` that points to these utf8 bytes. This is safe because `withUnsafeBufferPointer`
/// guarantees that the buffer pointer will be valid for the duration of the `withUnsafeBufferPointer`
/// callback.
/// 3. Pass the `RustStr` to the closure that was passed into `RustStr.toRustStr`.
public protocol ToRustStr {
func toRustStr<T> (_ withUnsafeRustStr: (RustStr) -> T) -> T;
}

extension String: ToRustStr {
/// Safely get a scoped pointer to the String and then call the callback with a RustStr
/// that uses that pointer.
Expand Down Expand Up @@ -125,6 +135,10 @@ public class RustVec<T: Vectorizable> {
T.vecOfSelfGet(vecPtr: ptr, index: index)
}

public func as_ptr() -> UnsafePointer<T.SelfRef> {
UnsafePointer<T.SelfRef>(OpaquePointer(T.vecOfSelfAsPtr(vecPtr: ptr)))
}

/// Rust returns a UInt, but we cast to an Int because many Swift APIs such as
/// `ForEach(0..rustVec.len())` expect Int.
public func len() -> Int {
Expand Down Expand Up @@ -203,6 +217,8 @@ public protocol Vectorizable {

static func vecOfSelfGetMut(vecPtr: UnsafeMutableRawPointer, index: UInt) -> Optional<SelfRefMut>

static func vecOfSelfAsPtr(vecPtr: UnsafeMutableRawPointer) -> UnsafePointer<SelfRef>

static func vecOfSelfLen(vecPtr: UnsafeMutableRawPointer) -> UInt
}

Expand Down Expand Up @@ -246,6 +262,10 @@ extension UInt8: Vectorizable {
}
}

public static func vecOfSelfAsPtr(vecPtr: UnsafeMutableRawPointer) -> UnsafePointer<Self> {
UnsafePointer<Self>(OpaquePointer(__swift_bridge__$Vec_u8$as_ptr(vecPtr)))
}

public static func vecOfSelfLen(vecPtr: UnsafeMutableRawPointer) -> UInt {
__swift_bridge__$Vec_u8$len(vecPtr)
}
Expand Down Expand Up @@ -291,6 +311,10 @@ extension UInt16: Vectorizable {
}
}

public static func vecOfSelfAsPtr(vecPtr: UnsafeMutableRawPointer) -> UnsafePointer<Self> {
UnsafePointer<Self>(OpaquePointer(__swift_bridge__$Vec_u16$as_ptr(vecPtr)))
}

public static func vecOfSelfLen(vecPtr: UnsafeMutableRawPointer) -> UInt {
__swift_bridge__$Vec_u16$len(vecPtr)
}
Expand Down Expand Up @@ -336,6 +360,10 @@ extension UInt32: Vectorizable {
}
}

public static func vecOfSelfAsPtr(vecPtr: UnsafeMutableRawPointer) -> UnsafePointer<Self> {
UnsafePointer<Self>(OpaquePointer(__swift_bridge__$Vec_u32$as_ptr(vecPtr)))
}

public static func vecOfSelfLen(vecPtr: UnsafeMutableRawPointer) -> UInt {
__swift_bridge__$Vec_u32$len(vecPtr)
}
Expand Down Expand Up @@ -381,6 +409,10 @@ extension UInt64: Vectorizable {
}
}

public static func vecOfSelfAsPtr(vecPtr: UnsafeMutableRawPointer) -> UnsafePointer<Self> {
UnsafePointer<Self>(OpaquePointer(__swift_bridge__$Vec_u64$as_ptr(vecPtr)))
}

public static func vecOfSelfLen(vecPtr: UnsafeMutableRawPointer) -> UInt {
__swift_bridge__$Vec_u64$len(vecPtr)
}
Expand Down Expand Up @@ -426,6 +458,10 @@ extension UInt: Vectorizable {
}
}

public static func vecOfSelfAsPtr(vecPtr: UnsafeMutableRawPointer) -> UnsafePointer<Self> {
UnsafePointer<Self>(OpaquePointer(__swift_bridge__$Vec_usize$as_ptr(vecPtr)))
}

public static func vecOfSelfLen(vecPtr: UnsafeMutableRawPointer) -> UInt {
__swift_bridge__$Vec_usize$len(vecPtr)
}
Expand Down Expand Up @@ -471,6 +507,10 @@ extension Int8: Vectorizable {
}
}

public static func vecOfSelfAsPtr(vecPtr: UnsafeMutableRawPointer) -> UnsafePointer<Self> {
UnsafePointer<Self>(OpaquePointer(__swift_bridge__$Vec_i8$as_ptr(vecPtr)))
}

public static func vecOfSelfLen(vecPtr: UnsafeMutableRawPointer) -> UInt {
__swift_bridge__$Vec_i8$len(vecPtr)
}
Expand Down Expand Up @@ -516,6 +556,10 @@ extension Int16: Vectorizable {
}
}

public static func vecOfSelfAsPtr(vecPtr: UnsafeMutableRawPointer) -> UnsafePointer<Self> {
UnsafePointer<Self>(OpaquePointer(__swift_bridge__$Vec_i16$as_ptr(vecPtr)))
}

public static func vecOfSelfLen(vecPtr: UnsafeMutableRawPointer) -> UInt {
__swift_bridge__$Vec_i16$len(vecPtr)
}
Expand Down Expand Up @@ -561,6 +605,10 @@ extension Int32: Vectorizable {
}
}

public static func vecOfSelfAsPtr(vecPtr: UnsafeMutableRawPointer) -> UnsafePointer<Self> {
UnsafePointer<Self>(OpaquePointer(__swift_bridge__$Vec_i32$as_ptr(vecPtr)))
}

public static func vecOfSelfLen(vecPtr: UnsafeMutableRawPointer) -> UInt {
__swift_bridge__$Vec_i32$len(vecPtr)
}
Expand Down Expand Up @@ -606,6 +654,10 @@ extension Int64: Vectorizable {
}
}

public static func vecOfSelfAsPtr(vecPtr: UnsafeMutableRawPointer) -> UnsafePointer<Self> {
UnsafePointer<Self>(OpaquePointer(__swift_bridge__$Vec_i64$as_ptr(vecPtr)))
}

public static func vecOfSelfLen(vecPtr: UnsafeMutableRawPointer) -> UInt {
__swift_bridge__$Vec_i64$len(vecPtr)
}
Expand Down Expand Up @@ -651,6 +703,10 @@ extension Int: Vectorizable {
}
}

public static func vecOfSelfAsPtr(vecPtr: UnsafeMutableRawPointer) -> UnsafePointer<Self> {
UnsafePointer<Self>(OpaquePointer(__swift_bridge__$Vec_isize$as_ptr(vecPtr)))
}

public static func vecOfSelfLen(vecPtr: UnsafeMutableRawPointer) -> UInt {
__swift_bridge__$Vec_isize$len(vecPtr)
}
Expand Down Expand Up @@ -696,6 +752,10 @@ extension Bool: Vectorizable {
}
}

public static func vecOfSelfAsPtr(vecPtr: UnsafeMutableRawPointer) -> UnsafePointer<Self> {
UnsafePointer<Self>(OpaquePointer(__swift_bridge__$Vec_bool$as_ptr(vecPtr)))
}

public static func vecOfSelfLen(vecPtr: UnsafeMutableRawPointer) -> UInt {
__swift_bridge__$Vec_bool$len(vecPtr)
}
Expand Down Expand Up @@ -741,6 +801,10 @@ extension Float: Vectorizable {
}
}

public static func vecOfSelfAsPtr(vecPtr: UnsafeMutableRawPointer) -> UnsafePointer<Self> {
UnsafePointer<Self>(OpaquePointer(__swift_bridge__$Vec_f32$as_ptr(vecPtr)))
}

public static func vecOfSelfLen(vecPtr: UnsafeMutableRawPointer) -> UInt {
__swift_bridge__$Vec_f32$len(vecPtr)
}
Expand Down Expand Up @@ -786,6 +850,10 @@ extension Double: Vectorizable {
}
}

public static func vecOfSelfAsPtr(vecPtr: UnsafeMutableRawPointer) -> UnsafePointer<Self> {
UnsafePointer<Self>(OpaquePointer(__swift_bridge__$Vec_f64$as_ptr(vecPtr)))
}

public static func vecOfSelfLen(vecPtr: UnsafeMutableRawPointer) -> UInt {
__swift_bridge__$Vec_f64$len(vecPtr)
}
Expand Down Expand Up @@ -886,6 +954,10 @@ extension RustString: Vectorizable {
}
}

public static func vecOfSelfAsPtr(vecPtr: UnsafeMutableRawPointer) -> UnsafePointer<RustStringRef> {
UnsafePointer<RustStringRef>(OpaquePointer(__swift_bridge__$Vec_RustString$as_ptr(vecPtr)))
}

public static func vecOfSelfLen(vecPtr: UnsafeMutableRawPointer) -> UInt {
__swift_bridge__$Vec_RustString$len(vecPtr)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import Foundation
import libem_proxy

public func start_emotional_damage<GenericToRustStr: ToRustStr>(_ bind_addr: GenericToRustStr) -> Optional<EMPError> {
return bind_addr.toRustStr({ bind_addrAsRustStr in
__swift_bridge__$start_emotional_damage(bind_addrAsRustStr).intoSwiftRepr()
Expand Down Expand Up @@ -96,6 +99,10 @@ extension EMPError: Vectorizable {
return maybeEnum.intoSwiftRepr()
}

public static func vecOfSelfAsPtr(vecPtr: UnsafeMutableRawPointer) -> UnsafePointer<Self> {
UnsafePointer<Self>(OpaquePointer(__swift_bridge__$Vec_EMPError$as_ptr(vecPtr)))
}

public static func vecOfSelfLen(vecPtr: UnsafeMutableRawPointer) -> UInt {
__swift_bridge__$Vec_EMPError$len(vecPtr)
}
Expand Down
File renamed without changes.
23 changes: 23 additions & 0 deletions Sources/libem_proxy/em_proxy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// File automatically generated by swift-bridge.
#include <stdint.h>
#include <stdbool.h>
#include "SwiftBridgeCore.h"

typedef enum __swift_bridge__$EMPErrorTag { __swift_bridge__$EMPError$InvalidAddress, __swift_bridge__$EMPError$InvalidPort, __swift_bridge__$EMPError$InvalidSocket, __swift_bridge__$EMPError$AlreadyRunning, __swift_bridge__$EMPError$Unknown, } __swift_bridge__$EMPErrorTag;
typedef struct __swift_bridge__$EMPError { __swift_bridge__$EMPErrorTag tag; } __swift_bridge__$EMPError;
typedef struct __swift_bridge__$Option$EMPError { bool is_some; __swift_bridge__$EMPError val; } __swift_bridge__$Option$EMPError;
void* __swift_bridge__$EMPError$Debug(__swift_bridge__$EMPError this);
void* __swift_bridge__$Vec_EMPError$new(void);
void __swift_bridge__$Vec_EMPError$drop(void* vec_ptr);
void __swift_bridge__$Vec_EMPError$push(void* vec_ptr, __swift_bridge__$EMPError item);
__swift_bridge__$Option$EMPError __swift_bridge__$Vec_EMPError$pop(void* vec_ptr);
__swift_bridge__$Option$EMPError __swift_bridge__$Vec_EMPError$get(void* vec_ptr, uintptr_t index);
__swift_bridge__$Option$EMPError __swift_bridge__$Vec_EMPError$get_mut(void* vec_ptr, uintptr_t index);
uintptr_t __swift_bridge__$Vec_EMPError$len(void* vec_ptr);
void* __swift_bridge__$Vec_EMPError$as_ptr(void* vec_ptr);

struct __swift_bridge__$Option$EMPError __swift_bridge__$start_emotional_damage(struct RustStr bind_addr);
void __swift_bridge__$stop_emotional_damage(void);
struct __swift_bridge__$Option$EMPError __swift_bridge__$test_emotional_damage(uint32_t timeout);


6 changes: 6 additions & 0 deletions Sources/libem_proxy/include/module.modulemap
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module libem_proxy {
header "../em_proxy.h"
header "../SwiftBridgeCore.h"

export *
}
18 changes: 18 additions & 0 deletions Tests/em_proxyTests/Test.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// Test.swift
// em_proxy
//
// Created by Joseph Mattiello on 9/16/24.
//

import Testing
@testable import em_proxy
@testable import libem_proxy

struct Test {

@Test func test_em_proxy() async throws {
// Write your test here and use APIs like `#expect(...)` to check expected conditions.
}

}
5 changes: 0 additions & 5 deletions module.modulemap

This file was deleted.

0 comments on commit a238687

Please sign in to comment.