Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions libcc2rs/src/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1328,12 +1328,14 @@ fn root_address(root: &SliceRoot) -> *const () {
// Moves the element at(order[i]) to at(i), for every i, as std::sort does
// once it knows the order. The elements are swapped, which moves them instead
// of copying them.
pub fn reorder<T: Clone + ByteRepr>(mut order: Vec<isize>, at: impl Fn(isize) -> Ptr<T>) {
pub fn reorder<T: ByteRepr>(mut order: Vec<isize>, at: impl Fn(isize) -> Ptr<T>) {
for i in 0..order.len() {
let mut cur = i;
while order[cur] as usize != i {
let next = order[cur] as usize;
at(cur as isize).swap_with(&at(next as isize), Ptr::read);
at(cur as isize).swap_with(&at(next as isize), |_| {
unreachable!("the elements of a range are in the same array")
});
order[cur] = cur as isize;
cur = next;
}
Expand Down
115 changes: 111 additions & 4 deletions tests/unit/out/refcount/sort_no_copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,37 @@ pub fn operator_lt_1(a: Ptr<Tracked>, b: Ptr<Tracked>) -> bool {
pub fn greater_2(a: Ptr<Tracked>, b: Ptr<Tracked>) -> bool {
return ({ a.with(|__s| __s.v) } > { b.with(|__s| __s.v) });
}
#[derive(Record, ByteRepr, FnPtrArg, MoveCtor, CppFrom, MoveAssign, Default)]
#[cpp_from(i32, new, value)]
#[byte_size(4)]
pub struct MoveOnly {
#[offset(0)]
pub v: i32,
}
impl MoveOnly {
pub fn new(mut v: i32) -> Self {
Self { v: v }
}
pub fn move_from(_a0: Ptr<MoveOnly>) -> Self {
Self {
v: { (*_a0.upgrade().deref()).v },
}
}
}
impl CppLess for MoveOnly {
fn less(__lhs: &Ptr<Self>, __rhs: &Ptr<Self>) -> bool {
operator_lt_3(__lhs.clone(), __rhs.clone())
}
}
impl CppEq for MoveOnly {
fn equal(__lhs: &Ptr<Self>, __rhs: &Ptr<Self>) -> bool {
!(operator_lt_3(__lhs.clone(), __rhs.clone()))
&& !(operator_lt_3(__rhs.clone(), __lhs.clone()))
}
}
pub fn operator_lt_3(a: Ptr<MoveOnly>, b: Ptr<MoveOnly>) -> bool {
return ({ a.with(|__s| __s.v) } < { b.with(|__s| __s.v) });
}
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, MoveCtor, MoveAssign, Default)]
#[byte_size(24)]
pub struct Bag {
Expand All @@ -80,18 +111,18 @@ impl Bag {
}
impl CppLess for Bag {
fn less(__lhs: &Ptr<Self>, __rhs: &Ptr<Self>) -> bool {
operator_lt_3(__lhs.clone(), __rhs.clone())
operator_lt_4(__lhs.clone(), __rhs.clone())
}
}
impl CppEq for Bag {
fn equal(__lhs: &Ptr<Self>, __rhs: &Ptr<Self>) -> bool {
operator_eq_4(__lhs.clone(), __rhs.clone())
operator_eq_5(__lhs.clone(), __rhs.clone())
}
}
pub fn operator_lt_3(a: Ptr<Bag>, b: Ptr<Bag>) -> bool {
pub fn operator_lt_4(a: Ptr<Bag>, b: Ptr<Bag>) -> bool {
return ({ (*a.upgrade().deref()).items.len() } < { (*b.upgrade().deref()).items.len() });
}
pub fn operator_eq_4(a: Ptr<Bag>, b: Ptr<Bag>) -> bool {
pub fn operator_eq_5(a: Ptr<Bag>, b: Ptr<Bag>) -> bool {
return ({ (*a.upgrade().deref()).items.len() } == { (*b.upgrade().deref()).items.len() });
}
pub fn main() {
Expand Down Expand Up @@ -390,6 +421,73 @@ fn main_0() -> i32 {
.with(|__m: &Map<Bag, (Value<Bag>, Value<i32>)>| __m.end())
);
assert!((copies_0.with(|rc| *rc.borrow()) == 0));
let moves: Value<Vec<MoveOnly>> = Rc::new(RefCell::new(Vec::new()));
{
let __init = MoveOnly::new({ 3 });
(*moves.borrow_mut()).push(__init)
};
{
let __init = MoveOnly::new({ 1 });
(*moves.borrow_mut()).push(__init)
};
{
let __init = MoveOnly::new({ 2 });
(*moves.borrow_mut()).push(__init)
};
match ({ moves.decay().begin() },) {
(mut __tmp_11,) => {
let __n = moves.decay().end().iter_distance(&__tmp_11);
let mut __order: Vec<isize> = (0..__n).collect();
__order.sort_by(|&__x, &__y| cpp_cmp(&__tmp_11.iter_at(__x), &__tmp_11.iter_at(__y)));
reorder(__order, |__i| __tmp_11.iter_at(__i));
}
};
assert!(
({
(({ (*moves.borrow())[0_usize].v } == 1) && ({ (*moves.borrow())[1_usize].v } == 2))
&& ({ (*moves.borrow())[2_usize].v } == 3)
})
);
match ({ moves.decay().begin() },) {
(mut __tmp_12,) => {
let __n = moves.decay().end().iter_distance(&__tmp_12);
let mut __order: Vec<isize> = (0..__n).collect();
__order.sort_by(|&__x, &__y| {
let __a = __tmp_12.iter_at(__x);
let __b = __tmp_12.iter_at(__y);
if FnPtr::<fn(Ptr<MoveOnly>, Ptr<MoveOnly>) -> bool>::new(
|a: Ptr<MoveOnly>, b: Ptr<MoveOnly>| -> bool {
{
return ({ a.with(|__s| __s.v) } > { b.with(|__s| __s.v) });
}
},
)
.call(__a.clone(), __b.clone())
{
std::cmp::Ordering::Less
} else if FnPtr::<fn(Ptr<MoveOnly>, Ptr<MoveOnly>) -> bool>::new(
|a: Ptr<MoveOnly>, b: Ptr<MoveOnly>| -> bool {
{
return ({ a.with(|__s| __s.v) } > { b.with(|__s| __s.v) });
}
},
)
.call(__b, __a)
{
std::cmp::Ordering::Greater
} else {
std::cmp::Ordering::Equal
}
});
reorder(__order, |__i| __tmp_12.iter_at(__i));
}
};
assert!(
({
(({ (*moves.borrow())[0_usize].v } == 3) && ({ (*moves.borrow())[1_usize].v } == 2))
&& ({ (*moves.borrow())[2_usize].v } == 1)
})
);
return copies_0.with(|rc| *rc.borrow());
}
pub trait BagImpl {
Expand All @@ -406,6 +504,15 @@ impl BagImpl for Ptr<Bag> {
return (*self).clone();
}
}
pub trait MoveOnlyImpl {
fn move_assign(&self, _a0: Ptr<MoveOnly>) -> Ptr<MoveOnly>;
}
impl MoveOnlyImpl for Ptr<MoveOnly> {
fn move_assign(&self, _a0: Ptr<MoveOnly>) -> Ptr<MoveOnly> {
field!((*self), v).write({ { (*_a0.upgrade().deref()).v } });
return (*self).clone();
}
}
pub trait TrackedImpl {
fn copy_assign(&self, o: Ptr<Tracked>) -> Ptr<Tracked>;
fn move_assign(&self, o: Ptr<Tracked>) -> Ptr<Tracked>;
Expand Down
135 changes: 130 additions & 5 deletions tests/unit/out/unsafe/sort_no_copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,56 @@ pub unsafe fn greater_2(a: *const Tracked, b: *const Tracked) -> bool {
return (((*a).v) > ((*b).v));
}
#[repr(C)]
#[derive(FnPtrArg, MoveCtorUnsafe, CppFromUnsafe, MoveAssignUnsafe, Default)]
#[cpp_from(i32, new, value)]
pub struct MoveOnly {
pub v: i32,
}
impl MoveOnly {
pub unsafe fn new(mut v: i32) -> Self {
let mut this = Self { v: v };
this
}
pub unsafe fn move_from(_a0: *mut MoveOnly) -> Self {
let mut this = Self { v: (*_a0).v };
this
}
pub unsafe fn move_assign(&mut self, _a0: *mut MoveOnly) -> *mut MoveOnly {
self.v = (*_a0).v;
return &mut (*(self as *mut MoveOnly));
}
}
impl std::cmp::Ord for MoveOnly {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
unsafe {
if operator_lt_3(self as *const MoveOnly, other as *const MoveOnly) {
std::cmp::Ordering::Less
} else if operator_lt_3(other as *const MoveOnly, self as *const MoveOnly) {
std::cmp::Ordering::Greater
} else {
std::cmp::Ordering::Equal
}
}
}
}
impl std::cmp::PartialOrd for MoveOnly {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl std::cmp::PartialEq for MoveOnly {
fn eq(&self, other: &Self) -> bool {
unsafe {
!(operator_lt_3(self as *const MoveOnly, other as *const MoveOnly))
&& !(operator_lt_3(other as *const MoveOnly, self as *const MoveOnly))
}
}
}
impl std::cmp::Eq for MoveOnly {}
pub unsafe fn operator_lt_3(a: *const MoveOnly, b: *const MoveOnly) -> bool {
return (((*a).v) < ((*b).v));
}
#[repr(C)]
#[derive(Clone, VaArg, FnPtrArg, MoveCtorUnsafe, MoveAssignUnsafe, Default)]
pub struct Bag {
pub items: Vec<Tracked>,
Expand All @@ -107,9 +157,9 @@ impl Bag {
impl std::cmp::Ord for Bag {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
unsafe {
if operator_lt_3(self as *const Bag, other as *const Bag) {
if operator_lt_4(self as *const Bag, other as *const Bag) {
std::cmp::Ordering::Less
} else if operator_lt_3(other as *const Bag, self as *const Bag) {
} else if operator_lt_4(other as *const Bag, self as *const Bag) {
std::cmp::Ordering::Greater
} else {
std::cmp::Ordering::Equal
Expand All @@ -124,14 +174,14 @@ impl std::cmp::PartialOrd for Bag {
}
impl std::cmp::PartialEq for Bag {
fn eq(&self, other: &Self) -> bool {
unsafe { operator_eq_4(self as *const Bag, other as *const Bag) }
unsafe { operator_eq_5(self as *const Bag, other as *const Bag) }
}
}
impl std::cmp::Eq for Bag {}
pub unsafe fn operator_lt_3(a: *const Bag, b: *const Bag) -> bool {
pub unsafe fn operator_lt_4(a: *const Bag, b: *const Bag) -> bool {
return (((*a).items.len()) < ((*b).items.len()));
}
pub unsafe fn operator_eq_4(a: *const Bag, b: *const Bag) -> bool {
pub unsafe fn operator_eq_5(a: *const Bag, b: *const Bag) -> bool {
return (((*a).items.len()) == ((*b).items.len()));
}
pub fn main() {
Expand Down Expand Up @@ -437,6 +487,81 @@ unsafe fn main_0() -> i32 {
assert!((((*bag_map.find(&bags[(2) as usize]).iter_deref()).1) == (2)));
assert!(bag_map.find(&bags[(1) as usize]) == bag_map.end());
assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut copies_0)) == (0)));
let mut moves: Vec<MoveOnly> = Vec::new();
{
let __init = MoveOnly::new({ 3 });
moves.push(__init)
};
{
let __init = MoveOnly::new({ 1 });
moves.push(__init)
};
{
let __init = MoveOnly::new({ 2 });
moves.push(__init)
};
match ({ moves.begin() },) {
(mut __tmp_11,) => {
let __n = moves.end().iter_distance(&__tmp_11);
let mut __order: Vec<isize> = (0..__n).collect();
__order.sort_by(|&__x, &__y| (*__tmp_11.iter_at(__x)).cmp(&*__tmp_11.iter_at(__y)));
let __values: Vec<_> = __order
.iter()
.map(|&__i| std::ptr::read(__tmp_11.iter_at(__i)))
.collect();
for (__i, __v) in (0..__n).zip(__values) {
std::ptr::write(__tmp_11.iter_at(__i), __v);
}
}
};
assert!(
(((moves[(0_usize)].v) == (1)) && ((moves[(1_usize)].v) == (2)))
&& ((moves[(2_usize)].v) == (3))
);
match ({ moves.begin() },) {
(mut __tmp_12,) => {
let __n = moves.end().iter_distance(&__tmp_12);
let mut __order: Vec<isize> = (0..__n).collect();
__order.sort_by(|&__x, &__y| {
let __a = __tmp_12.iter_at(__x) as *const _;
let __b = __tmp_12.iter_at(__y) as *const _;
if FnPtr::<fn(*const MoveOnly, *const MoveOnly) -> bool>::new(
|a: *const MoveOnly, b: *const MoveOnly| -> bool {
unsafe {
return (((*a).v) > ((*b).v));
}
},
)
.call(__a, __b)
{
std::cmp::Ordering::Less
} else if FnPtr::<fn(*const MoveOnly, *const MoveOnly) -> bool>::new(
|a: *const MoveOnly, b: *const MoveOnly| -> bool {
unsafe {
return (((*a).v) > ((*b).v));
}
},
)
.call(__b, __a)
{
std::cmp::Ordering::Greater
} else {
std::cmp::Ordering::Equal
}
});
let __values: Vec<_> = __order
.iter()
.map(|&__i| std::ptr::read(__tmp_12.iter_at(__i)))
.collect();
for (__i, __v) in (0..__n).zip(__values) {
std::ptr::write(__tmp_12.iter_at(__i), __v);
}
}
};
assert!(
(((moves[(0_usize)].v) == (3)) && ((moves[(1_usize)].v) == (2)))
&& ((moves[(2_usize)].v) == (1))
);
return (*std::cell::LazyCell::force_mut(&mut *&raw mut copies_0));
}
pub unsafe fn __cpp2rust_init_globals() {
Expand Down
21 changes: 21 additions & 0 deletions tests/unit/sort_no_copy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ bool operator<(const Tracked &a, const Tracked &b) { return a.v < b.v; }

bool greater(const Tracked &a, const Tracked &b) { return a.v > b.v; }

struct MoveOnly {
int v;
MoveOnly(int v) : v(v) {}
MoveOnly(MoveOnly &&) = default;
MoveOnly &operator=(MoveOnly &&) = default;
MoveOnly(const MoveOnly &) = delete;
};

bool operator<(const MoveOnly &a, const MoveOnly &b) { return a.v < b.v; }

struct Bag {
std::vector<Tracked> items;
};
Expand Down Expand Up @@ -101,5 +111,16 @@ int main() {
assert(bag_map.find(bags[2])->second == 2);
assert(bag_map.find(bags[1]) == bag_map.end());
assert(copies == 0);

std::vector<MoveOnly> moves;
moves.emplace_back(3);
moves.emplace_back(1);
moves.emplace_back(2);
std::sort(moves.begin(), moves.end());
assert(moves[0].v == 1 && moves[1].v == 2 && moves[2].v == 3);
std::stable_sort(
moves.begin(), moves.end(),
[](const MoveOnly &a, const MoveOnly &b) { return a.v > b.v; });
assert(moves[0].v == 3 && moves[1].v == 2 && moves[2].v == 1);
return copies;
}
Loading