Skip to content

Commit

Permalink
Optimize RustStr's Equatable implementation (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
NiwakaDev committed Jan 29, 2023
1 parent 0de0dc8 commit 5428646
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,26 @@ class StringTests: XCTestCase {
"hi"
)
}

func testRustStrEqualityOperator() throws {
XCTContext.runActivity(named: "Should be equal"){
_ in
let hello1 = create_string("hello")
let hello2 = create_string("hello")
XCTAssertEqual(hello1.as_str(), hello2.as_str())
}
XCTContext.runActivity(named: "Should not be equal"){
_ in
//Not equal length
let hi = create_string("hi")
let hello = create_string("hello")
XCTAssertNotEqual(hi.as_str(), hello.as_str())

//Equal length
let foo = create_string("foo")
let bar = create_string("bar")
XCTAssertNotEqual(foo.as_str(), bar.as_str())
}
}
}

3 changes: 2 additions & 1 deletion crates/swift-bridge-build/src/generate_core/rust_string.c.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ void* __swift_bridge__$RustString$new(void);
void* __swift_bridge__$RustString$new_with_str(struct RustStr str);
uintptr_t __swift_bridge__$RustString$len(void* self);
struct RustStr __swift_bridge__$RustString$as_str(void* self);
struct RustStr __swift_bridge__$RustString$trim(void* self);
struct RustStr __swift_bridge__$RustString$trim(void* self);
bool __swift_bridge__$RustStr$partial_eq(struct RustStr lhs, struct RustStr rhs);
5 changes: 1 addition & 4 deletions crates/swift-bridge-build/src/generate_core/string.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ extension RustStr: Identifiable {
}
extension RustStr: Equatable {
public static func == (lhs: RustStr, rhs: RustStr) -> Bool {
// TODO: Rather than compare Strings, we can avoid allocating by calling a function
// on the Rust side that compares the underlying byte slices.
return
lhs.toString() == rhs.toString()
return __swift_bridge__$RustStr$partial_eq(lhs, rhs);
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/std_bridge.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! The corresponding C and Swift code can be found in
//! crates/swift-bridge-build/src/generate_core/*
#![allow(missing_docs)]

pub mod option;
Expand Down
17 changes: 17 additions & 0 deletions src/std_bridge/string.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! The corresponding C and Swift code can be found in
//! crates/swift-bridge-build/src/generate_core/rust_string.{c.h,swift}
pub use self::ffi::*;

#[swift_bridge_macro::bridge(swift_bridge_path = crate)]
Expand Down Expand Up @@ -80,3 +82,18 @@ impl RustStr {
}
}
}

impl PartialEq for RustStr {
fn eq(&self, other: &Self) -> bool {
unsafe {
std::slice::from_raw_parts(self.start, self.len)
== std::slice::from_raw_parts(other.start, other.len)
}
}
}

#[export_name = "__swift_bridge__$RustStr$partial_eq"]
#[allow(non_snake_case)]
pub extern "C" fn __swift_bridge__RustStr_partial_eq(lhs: RustStr, rhs: RustStr) -> bool {
lhs == rhs
}

0 comments on commit 5428646

Please sign in to comment.