Skip to content

Commit 2a58a8a

Browse files
authored
Merge pull request #646 from dtolnay/swap
Restore swapping Str and Slice in member function form only
2 parents 815ca8c + 0413ee2 commit 2a58a8a

File tree

4 files changed

+15
-0
lines changed

4 files changed

+15
-0
lines changed

book/src/binding/slice.md

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ public:
4040
class iterator;
4141
iterator begin() const noexcept;
4242
iterator end() const noexcept;
43+
44+
void swap(Slice &) noexcept;
4345
};
4446
#
4547
# template <typename T>

book/src/binding/str.md

+2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ public:
4444
bool operator<=(const Str &) const noexcept;
4545
bool operator>(const Str &) const noexcept;
4646
bool operator>=(const Str &) const noexcept;
47+
48+
void swap(Str &) noexcept;
4749
};
4850
4951
std::ostream &operator<<(std::ostream &, const Str &);

include/cxx.h

+9
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ class Str final {
125125
bool operator>(const Str &) const noexcept;
126126
bool operator>=(const Str &) const noexcept;
127127

128+
void swap(Str &) noexcept;
129+
128130
private:
129131
std::array<std::uintptr_t, 2> repr;
130132
};
@@ -175,6 +177,8 @@ class Slice final
175177
iterator begin() const noexcept;
176178
iterator end() const noexcept;
177179

180+
void swap(Slice &) noexcept;
181+
178182
private:
179183
// Not necessarily ABI compatible with &[T]. Codegen will translate to
180184
// cxx::rust_slice::RustSlice which matches this layout.
@@ -666,6 +670,11 @@ typename Slice<T>::iterator Slice<T>::end() const noexcept {
666670
it.pos = static_cast<char *>(it.pos) + it.stride * this->len;
667671
return it;
668672
}
673+
674+
template <typename T>
675+
void Slice<T>::swap(Slice &rhs) noexcept {
676+
std::swap(*this, rhs);
677+
}
669678
#endif // CXXBRIDGE1_RUST_SLICE
670679

671680
#ifndef CXXBRIDGE1_RUST_BOX

src/cxx.cc

+2
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,8 @@ bool Str::operator>(const Str &rhs) const noexcept { return rhs < *this; }
267267

268268
bool Str::operator>=(const Str &rhs) const noexcept { return rhs <= *this; }
269269

270+
void Str::swap(Str &rhs) noexcept { std::swap(*this, rhs); }
271+
270272
std::ostream &operator<<(std::ostream &os, const Str &s) {
271273
os.write(s.data(), s.size());
272274
return os;

0 commit comments

Comments
 (0)