Skip to content

Commit

Permalink
Use explicit self references in generated swift
Browse files Browse the repository at this point in the history
* resolve errors in strict swift environments to
  disambiguate between local vars
  • Loading branch information
brittlewis12 committed Oct 14, 2024
1 parent c45a38c commit 6d26ade
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class __private__RustFnOnceCallbackNoArgsNoRet {
deinit {
if !called {
__swift_bridge__$free_boxed_fn_once_no_args_no_return(ptr)
__swift_bridge__$free_boxed_fn_once_no_args_no_return(self.ptr)
}
}
Expand All @@ -22,7 +22,7 @@ public class __private__RustFnOnceCallbackNoArgsNoRet {
fatalError("Cannot call a Rust FnOnce function twice")
}
called = true
return __swift_bridge__$call_boxed_fn_once_no_args_no_return(ptr)
return __swift_bridge__$call_boxed_fn_once_no_args_no_return(self.ptr)
}
}
"#;
Expand Down
8 changes: 4 additions & 4 deletions crates/swift-bridge-build/src/generate_core/rust_string.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class RustString: RustStringRefMut {

deinit {
if isOwned {
__swift_bridge__$RustString$_free(ptr)
__swift_bridge__$RustString$_free(self.ptr)
}
}
}
Expand Down Expand Up @@ -36,15 +36,15 @@ public class RustStringRef {
}
extension RustStringRef {
public func len() -> UInt {
__swift_bridge__$RustString$len(ptr)
__swift_bridge__$RustString$len(self.ptr)
}

public func as_str() -> RustStr {
__swift_bridge__$RustString$as_str(ptr)
__swift_bridge__$RustString$as_str(self.ptr)
}

public func trim() -> RustStr {
__swift_bridge__$RustString$trim(ptr)
__swift_bridge__$RustString$trim(self.ptr)
}
}
/// exercised in SwiftRustIntegrationTestRunner/SwiftRustIntegrationTestRunnerTests/ResultTests.swift:
Expand Down
18 changes: 9 additions & 9 deletions crates/swift-bridge-build/src/generate_core/rust_vec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,31 @@ public class RustVec<T: Vectorizable> {
isOwned = true
}

public func push (value: T) {
T.vecOfSelfPush(vecPtr: ptr, value: value)
public func push(value: T) {
T.vecOfSelfPush(vecPtr: self.ptr, value: value)
}

public func pop () -> Optional<T> {
T.vecOfSelfPop(vecPtr: ptr)
public func pop() -> Optional<T> {
T.vecOfSelfPop(vecPtr: self.ptr)
}

public func get(index: UInt) -> Optional<T.SelfRef> {
T.vecOfSelfGet(vecPtr: ptr, index: index)
T.vecOfSelfGet(vecPtr: self.ptr, index: index)
}

public func as_ptr() -> UnsafePointer<T.SelfRef> {
UnsafePointer<T.SelfRef>(OpaquePointer(T.vecOfSelfAsPtr(vecPtr: ptr)))
UnsafePointer<T.SelfRef>(OpaquePointer(T.vecOfSelfAsPtr(vecPtr: self.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 {
Int(T.vecOfSelfLen(vecPtr: ptr))
Int(T.vecOfSelfLen(vecPtr: self.ptr))
}

deinit {
if isOwned {
T.vecOfSelfFree(vecPtr: ptr)
T.vecOfSelfFree(vecPtr: self.ptr)
}
}
}
Expand Down Expand Up @@ -84,7 +84,7 @@ extension RustVec: Collection {
extension RustVec: RandomAccessCollection {}

extension UnsafeBufferPointer {
func toFfiSlice () -> __private__FfiSlice {
func toFfiSlice() -> __private__FfiSlice {
__private__FfiSlice(start: UnsafeMutablePointer(mutating: self.baseAddress), len: UInt(self.count))
}
}
Expand Down

0 comments on commit 6d26ade

Please sign in to comment.