Skip to content

Commit

Permalink
Add vecOfSelfAsPtr method, fixes #214
Browse files Browse the repository at this point in the history
  • Loading branch information
aiongg committed Apr 24, 2023
1 parent 1f9dd05 commit 53ab502
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ class VecTests: XCTestCase {
vec.push(value: 222)
XCTAssertEqual(vec.get(index: 1), 222)
}
func testRustVecU8AsPtr() throws {
let vec = RustVec<UInt8>()
vec.push(value: 10)
let ptr = vec.as_ptr()
XCTAssertEqual(ptr.pointee, 10)
}
func testRustVecU8Iterator() throws {
let vec = RustVec<UInt8>()
vec.push(value: 111)
Expand Down
6 changes: 6 additions & 0 deletions crates/swift-bridge-build/src/generate_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ fn conform_to_vectorizable(swift_ty: &str, rust_ty: &str) -> String {
format!(
r#"
extension {swift_ty}: Vectorizable {{
public typealias Elem = {swift_ty}
public static func vecOfSelfNew() -> UnsafeMutableRawPointer {{
__swift_bridge__$Vec_{rust_ty}$new()
}}
Expand Down Expand Up @@ -182,6 +184,10 @@ extension {swift_ty}: Vectorizable {{
}}
}}
public static func vecOfSelfAsPtr(vecPtr: UnsafeMutableRawPointer) -> UnsafePointer<Elem> {{
UnsafePointer<Elem>(OpaquePointer(__swift_bridge__$Vec_{rust_ty}$as_ptr(vecPtr)))
}}
public static func vecOfSelfLen(vecPtr: UnsafeMutableRawPointer) -> UInt {{
__swift_bridge__$Vec_{rust_ty}$len(vecPtr)
}}
Expand Down
4 changes: 4 additions & 0 deletions crates/swift-bridge-build/src/generate_core/rust_string.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ extension RustString: Vectorizable {
}
}

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

public static func vecOfSelfLen(vecPtr: UnsafeMutableRawPointer) -> UInt {
__swift_bridge__$Vec_RustString$len(vecPtr)
}
Expand Down
8 changes: 8 additions & 0 deletions crates/swift-bridge-build/src/generate_core/rust_vec.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
public class RustVec<T: Vectorizable> {
public typealias Elem = T
var ptr: UnsafeMutableRawPointer
var isOwned: Bool = true

Expand All @@ -23,6 +24,10 @@ public class RustVec<T: Vectorizable> {
T.vecOfSelfGet(vecPtr: ptr, index: index)
}

public func as_ptr() -> UnsafePointer<T> {
UnsafePointer<T>(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 @@ -86,6 +91,7 @@ extension UnsafeBufferPointer {
}

public protocol Vectorizable {
associatedtype Elem
associatedtype SelfRef
associatedtype SelfRefMut

Expand All @@ -101,5 +107,7 @@ public protocol Vectorizable {

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

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

static func vecOfSelfLen(vecPtr: UnsafeMutableRawPointer) -> UInt
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ mod extern_rust_type_vec_support {
ExpectedSwiftCode::ContainsAfterTrim(
r#"
extension MyRustType: Vectorizable {
public typealias Elem = MyRustType
public static func vecOfSelfNew() -> UnsafeMutableRawPointer {
__swift_bridge__$Vec_MyRustType$new()
}
Expand Down Expand Up @@ -132,6 +134,10 @@ extension MyRustType: Vectorizable {
}
}
public static func vecOfSelfAsPtr(vecPtr: UnsafeMutableRawPointer) -> UnsafePointer<Elem> {
UnsafePointer<Elem>(OpaquePointer(__swift_bridge__$Vec_MyRustType$as_ptr(vecPtr)))
}
public static func vecOfSelfLen(vecPtr: UnsafeMutableRawPointer) -> UInt {
__swift_bridge__$Vec_MyRustType$len(vecPtr)
}
Expand Down Expand Up @@ -360,6 +366,8 @@ mod transparent_enum_vec_support {
ExpectedSwiftCode::ContainsAfterTrim(
r#"
extension SomeEnum: Vectorizable {
public typealias Elem = SomeEnum
public static func vecOfSelfNew() -> UnsafeMutableRawPointer {
__swift_bridge__$Vec_SomeEnum$new()
}
Expand Down Expand Up @@ -387,6 +395,10 @@ extension SomeEnum: Vectorizable {
return maybeEnum.intoSwiftRepr()
}
public static func vecOfSelfAsPtr(vecPtr: UnsafeMutableRawPointer) -> UnsafePointer<Elem> {
UnsafePointer<Elem>(OpaquePointer(__swift_bridge__$Vec_SomeEnum$as_ptr(vecPtr)))
}
public static func vecOfSelfLen(vecPtr: UnsafeMutableRawPointer) -> UInt {
__swift_bridge__$Vec_SomeEnum$len(vecPtr)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ impl SwiftBridgeModule {
format!(
r#"
extension {enum_name}: Vectorizable {{
public typealias Elem = {enum_name}
public static func vecOfSelfNew() -> UnsafeMutableRawPointer {{
__swift_bridge__$Vec_{enum_name}$new()
}}
Expand Down Expand Up @@ -123,6 +125,10 @@ extension {enum_name}: Vectorizable {{
return maybeEnum.intoSwiftRepr()
}}
public static func vecOfSelfAsPtr(vecPtr: UnsafeMutableRawPointer) -> UnsafePointer<Elem> {{
UnsafePointer<Elem>(OpaquePointer(__swift_bridge__$Vec_{enum_name}$as_ptr(vecPtr)))
}}
public static func vecOfSelfLen(vecPtr: UnsafeMutableRawPointer) -> UInt {{
__swift_bridge__$Vec_{enum_name}$len(vecPtr)
}}
Expand Down
12 changes: 12 additions & 0 deletions crates/swift-bridge-ir/src/codegen/generate_swift/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use proc_macro2::Ident;
pub(super) fn generate_vectorizable_extension(ty: &Ident) -> String {
format!(
r#"extension {ty}: Vectorizable {{
public typealias Elem = {ty}
public static func vecOfSelfNew() -> UnsafeMutableRawPointer {{
__swift_bridge__$Vec_{ty}$new()
}}
Expand Down Expand Up @@ -43,6 +45,10 @@ pub(super) fn generate_vectorizable_extension(ty: &Ident) -> String {
}}
}}
public static func vecOfSelfAsPtr(vecPtr: UnsafeMutableRawPointer) -> UnsafePointer<Elem> {{
UnsafePointer<Elem>(OpaquePointer(__swift_bridge__$Vec_{ty}$as_ptr(vecPtr)))
}}
public static func vecOfSelfLen(vecPtr: UnsafeMutableRawPointer) -> UInt {{
__swift_bridge__$Vec_{ty}$len(vecPtr)
}}
Expand All @@ -64,6 +70,8 @@ mod tests {
fn generates_vectorizable_extension() {
let expected = r#"
extension ARustType: Vectorizable {
public typealias Elem = ARustType
public static func vecOfSelfNew() -> UnsafeMutableRawPointer {
__swift_bridge__$Vec_ARustType$new()
}
Expand Down Expand Up @@ -103,6 +111,10 @@ extension ARustType: Vectorizable {
}
}
public static func vecOfSelfAsPtr(vecPtr: UnsafeMutableRawPointer) -> UnsafePointer<Elem> {
UnsafePointer<Elem>(OpaquePointer(__swift_bridge__$Vec_ARustType$as_ptr(vecPtr)))
}
public static func vecOfSelfLen(vecPtr: UnsafeMutableRawPointer) -> UInt {
__swift_bridge__$Vec_ARustType$len(vecPtr)
}
Expand Down
6 changes: 6 additions & 0 deletions src/std_bridge/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ mod ffi {

fn as_str(&self) -> &str;

fn as_ptr(&self) -> *const u8;

fn trim(&self) -> &str;
}
}
Expand Down Expand Up @@ -48,6 +50,10 @@ impl RustString {
self.0.as_str()
}

fn as_ptr(&self) -> *const u8 {
self.0.as_ptr()
}

fn trim(&self) -> &str {
self.0.trim()
}
Expand Down

0 comments on commit 53ab502

Please sign in to comment.