File tree 1 file changed +13
-4
lines changed
crates/swift-bridge-build/src/generate_core
1 file changed +13
-4
lines changed Original file line number Diff line number Diff line change @@ -35,10 +35,6 @@ public protocol IntoRustString {
35
35
func intoRustString( ) -> RustString ;
36
36
}
37
37
38
- public protocol ToRustStr {
39
- func toRustStr< T> ( _ withUnsafeRustStr: ( RustStr ) -> T ) -> T ;
40
- }
41
-
42
38
extension String : IntoRustString {
43
39
public func intoRustString( ) -> RustString {
44
40
// TODO: When passing an owned Swift std String to Rust we've being wasteful here in that
@@ -72,6 +68,19 @@ func optionalStringIntoRustString<S: IntoRustString>(_ string: Optional<S>) -> R
72
68
}
73
69
}
74
70
71
+ /// Used to safely get a pointer to a sequence of utf8 bytes, represented as a `RustStr`.
72
+ ///
73
+ /// For example, the Swift `String` implementation of the `ToRustStr` protocol does the following:
74
+ /// 1. Use Swift's `String.utf8.withUnsafeBufferPointer` to get a pointer to the strings underlying
75
+ /// utf8 bytes.
76
+ /// 2. Construct a `RustStr` that points to these utf8 bytes. This is safe because `withUnsafeBufferPointer`
77
+ /// guarantees that the buffer pointer will be valid for the duration of the `withUnsafeBufferPointer`
78
+ /// callback.
79
+ /// 3. Pass the `RustStr` to the closure that was passed into `RustStr.toRustStr`.
80
+ public protocol ToRustStr {
81
+ func toRustStr< T> ( _ withUnsafeRustStr: ( RustStr ) -> T ) -> T ;
82
+ }
83
+
75
84
extension String : ToRustStr {
76
85
/// Safely get a scoped pointer to the String and then call the callback with a RustStr
77
86
/// that uses that pointer.
You can’t perform that action at this time.
0 commit comments