-
Notifications
You must be signed in to change notification settings - Fork 65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add RustVec<T>.as_ptr
method
#216
Add RustVec<T>.as_ptr
method
#216
Conversation
func testRustVecU8AsPtr() throws { | ||
let vec = RustVec<UInt8>() | ||
vec.push(value: 10) | ||
let ptr = vec.as_ptr() | ||
XCTAssertEqual(ptr.pointee, 10) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Basic test as described in the issue.
public typealias Elem = {swift_ty} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add the new typealias
up top, and add the method implementation below (I put it after the get
methods) - it's the same procedure for all of these generated code segments.
public static func vecOfSelfAsPtr(vecPtr: UnsafeMutableRawPointer) -> UnsafePointer<Elem> {{ | ||
UnsafePointer<Elem>(OpaquePointer(__swift_bridge__$Vec_{rust_ty}$as_ptr(vecPtr))) | ||
}} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We just leave the type parameter as Elem
in line with the Vectorizable
protocol, and all we have to do is change the method name to call the appropriate $Vec_{rust_ty}$as_ptr
function.
public static func vecOfSelfAsPtr(vecPtr: UnsafeMutableRawPointer) -> UnsafePointer<UInt8> { | ||
UnsafePointer<UInt8>(OpaquePointer(__swift_bridge__$Vec_RustString$as_ptr(vecPtr))) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Special handling for RustString
, which takes a UInt8
type parameter instead of the usual Self
.
src/std_bridge/string.rs
Outdated
fn as_ptr(&self) -> *const u8 { | ||
self.0.as_ptr() | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Implementation for RustString
. This is the *const u8
that maps to UnsafePointer<UInt8>
in swift.
associatedtype Elem | ||
associatedtype SelfRef | ||
associatedtype SelfRefMut |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new associatedtype Elem
for the Vectorizable
protocol
Thanks for submitting! Will review tonight or tomorrow |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.as_ptr()
for a RustVec<RustString>
should not return an UnsafePointer<UInt8>
, but rather an UnsafePointer<RustStringRef>
.
Consider the following Rust code:
let strings: Vec<String> = vec![];
let ptr: *const String = strings.as_ptr();
Vec<String>.as_ptr()
returns a *const String
.
Maybe you were accidentally looking at String.as_ptr()
, which is unrelated to this PR.
So, I think we can remove the Elem
associated type and instead just have the existing SelfRef
associated type
associatedtype SelfRef |
RustVec<RustString>
is a RustStringRef
public static func vecOfSelfGet(vecPtr: UnsafeMutableRawPointer, index: UInt) -> Optional<RustStringRef> { |
Ok great I'll try it. I think I tried that the first time and encountered some issue, but if you think it should work then I must have done something wrong so I'll try again and update the PR if it works. |
Mmm I'm not certain that it'll work exactly as I described it but it seems directionally correct. Let me know if you run into an issue. |
@chinedufn you were right, I was confusing |
Thanks looks good. All that's missing is to update your PR body with quick example code showing of what this PR now enables. Here's an example from another PR that you can use as inspiration #189 These examples get used in our release notes, and they help folks that are looking at old PRs understand exactly what the PR does. Once you add an example (and perhaps clean up the info in your PR body that is no longer true) we can merge this! |
@chinedufn done, please review. |
Thanks! FYI for PRs you can just show bridge module and an example of using that module like this: // Rust
#[swift_bridge::bridge]
mod ffi {
extern "Rust" {
fn send_serialized_proto(bytes: &[u8]) -> Option<Vec<u8>>;
}
} // Swift
// ... then you could've put your example of using it here ... |
This PR adds a method to the
Vectorizable
protocol:My reason for adding this method was to be able to easily pass a serialized Protobuf object back and forth between Rust and Swift. Motivating example:
Without
.as_ptr()
, it was necessary to return the raw pointer from Rust, which meant also returning the length separately, so using the Rust method was a bit less ergonomic on both sides. Here's how it looks now: