Skip to content
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.intoArray() #239

Open
naturecodevoid opened this issue Aug 18, 2023 · 1 comment
Open

Add RustVec.intoArray() #239

naturecodevoid opened this issue Aug 18, 2023 · 1 comment

Comments

@naturecodevoid
Copy link
Contributor

Hi,

I think it would be quite useful if RustVec had a builtin intoArray method. For example, I had issues using a RustVec in SwiftUI's List (I needed to use the List constructor that takes a children key path).

I was able to fix my issues with this simple extension:

extension RustVec {
    func intoArray() -> [T] {
        var array: [T] = []
        for _ in 0..<self.len() {
            array.append(self.pop()!)
        }
        return array.reversed()
    }
}

It should probably return nil if the RustVec isn't owned.

@chinedufn
Copy link
Owner

Good idea.

It should probably return nil if the RustVec isn't owned.

It isn't possible to borrow a RustVec today.

In the future when we supported borrowing we would have RustVecRef and RustVecRefMut. Similar to

## Owned, Ref and RefMut
When you define a type in an `extern "Rust"` block, three corresponding Swift classes get generated.
```swift
// Equivalent to `SomeType` in Rust
class SomeType: SomeTypeRefMut {
// ...
}
// Equivalent to `&mut SomeType` in Rust
class SomeTypeRefMut: SomeTypeRef {
// ...
}
// Equivalent to `&SomeType` in Rust
class SomeTypeRef {
// ...
}
```
Here's an example of how `&Type` and `&mut Type` are enforced:
```rust
// Rust
extern "Rust" {
type SomeType;
#[swift_bridge(init)]
fn new() -> SomeType;
// Callable by SomeType, SomeTypeRef and SomeTypeRefMut.
fn (&self) everyone();
// Callable by SomeType, and SomeTypeRefMut.
fn (&mut self) only_owned_and_ref_mut();
// Only callable by SomeType.
fn (self) only_owned();
}
extern "Rust" {
fn make_ref() -> &'static SomeType;
fn make_ref_mut() -> &'static mut SomeType;
}
```
```swift
// Swift
func methods() {
let someType: SomeType = SomeType()
let someTypeRef: SomeTypeRef = make_ref()
let someTypeRefMut: SomeTypeRefMut = make_ref_mut()
someType.everyone()
someType.only_owned_and_ref_mut()
someType.only_owned()
someTypeRefMut.everyone()
someTypeRefMut.only_owned_and_ref_mut()
someTypeRef.everyone()
}
func functions() {
let someType: SomeType = SomeType()
let someTypeRef: SomeTypeRef = make_ref()
let someTypeRefMut: SomeTypeRefMut = make_ref_mut()
takeReference(someType)
takeReference(someTypeRef)
takeReference(someTypeRefMut)
}
// Can be called with SomeType, SomeTypeRef and SomeTypeRefMut
func useSomeType(someType: SomeTypeRef) {
// ...
}
```

RustVecRef and RustVecRefMut wouldn't have the intoArray method so it wouldn't be possible to call it on a borrowed Rust vec.


If you're interested I'd be happy to merge a PR that adds this intoArray implementation.

Would just leave a TODO to enforce ownership (as in intoArray invalidates the RustVec) after #155 lands

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants