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
4 changes: 4 additions & 0 deletions rules/vector/tgt_refcount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ fn f96<T1>(a0: Ptr<T1>) -> RandomAccessIterator<T1> {
a0.end()
}

fn f97<T1: ByteRepr>(a0: Ptr<Vec<T1>>, a1: Ptr<Vec<T1>>) {
a0.swap_with(&a1, |__v| __v.with_mut(std::mem::take))
}

fn f98<T1: ByteRepr + 'static>(a0: Ptr<Vec<T1>>, a1: usize) -> Ptr<T1> {
if a1 as usize >= (*a0.upgrade().deref()).len() {
panic!("out of bounds access")
Expand Down
2 changes: 1 addition & 1 deletion rules/vector/tgt_unsafe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ unsafe fn f96<T1>(a0: Vec<T1>) -> UnsafeRandomAccessIterator<T1> {
a0.end()
}

unsafe fn f97(a0: &mut Vec<bool>, a1: &mut Vec<bool>) {
unsafe fn f97<T1>(a0: &mut Vec<T1>, a1: &mut Vec<T1>) {
std::mem::swap(&mut *a0, &mut *a1)
}

Expand Down
84 changes: 84 additions & 0 deletions tests/unit/out/refcount/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,27 @@ pub struct S {
pub x: i32,
}
pub fn copy_0(mut copy_vector: Vec<i32>) {}
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, MoveCtor, MoveAssign, Default)]
#[byte_size(48)]
pub struct Foo {
#[offset(0)]
#[byte_size(24)]
pub a: Vec<i32>,
#[offset(24)]
#[byte_size(24)]
pub b: Vec<i32>,
}
impl Foo {
pub fn move_from(_a0: Ptr<Foo>) -> Self {
Self {
a: field!(_a0, a).with_mut(std::mem::take),
b: field!(_a0, b).with_mut(std::mem::take),
}
}
}
pub fn exchange_1(x: Ptr<Vec<i32>>, y: Ptr<Vec<i32>>) {
x.swap_with(&(y).clone(), |__v| __v.with_mut(std::mem::take));
}
pub fn main() {
__cpp2rust_init_globals();
std::process::exit(main_0());
Expand Down Expand Up @@ -332,6 +353,69 @@ fn main_0() -> i32 {
};
field!(((v9[0_usize].read()).read()), x).write(2);
assert!(({ ({ (*a.borrow()).x } == 2) }));
let v10: Value<Vec<i32>> = Rc::new(RefCell::new(Vec::from([1, 2, 3])));
let v11: Value<Vec<i32>> = Rc::new(RefCell::new(Vec::from([4])));
v10.as_pointer()
.swap_with(&v11.as_pointer(), |__v| __v.with_mut(std::mem::take));
assert!(
({
((((*v10.borrow()).len() == 1_usize) && ({ (*v10.borrow())[0_usize] } == 4))
&& ((*v11.borrow()).len() == 3_usize))
&& ({ (*v11.borrow())[2_usize] } == 3)
})
);
let foo: Value<Foo> = Rc::new(RefCell::new(<Foo>::default()));
{
let __a1 = 5;
(*foo.borrow_mut()).a.push(__a1)
};
{
let __a1 = 6;
(*foo.borrow_mut()).b.push(__a1)
};
{
let __a1 = 7;
(*foo.borrow_mut()).b.push(__a1)
};
field_ref!(foo.as_pointer(), a)
.as_pointer()
.swap_with(&field_ref!(foo.as_pointer(), b).as_pointer(), |__v| {
__v.with_mut(std::mem::take)
});
assert!(
({
(((*foo.borrow()).a.len() == 2_usize)
&& ((vec_elem!(foo.as_pointer(), a, 1_usize).read()) == 7))
&& ((vec_elem!(foo.as_pointer(), b, 0_usize).read()) == 5)
})
);
({
exchange_1(
field_ref!(foo.as_pointer(), a).as_pointer(),
v10.as_pointer(),
)
});
assert!(
({
(((*foo.borrow()).a.len() == 1_usize)
&& ((vec_elem!(foo.as_pointer(), a, 0_usize).read()) == 4))
&& ({ (*v10.borrow())[1_usize] } == 7)
})
);
return 0;
}
pub trait FooImpl {
fn move_assign(&self, _a0: Ptr<Foo>) -> Ptr<Foo>;
}
impl FooImpl for Ptr<Foo> {
fn move_assign(&self, _a0: Ptr<Foo>) -> Ptr<Foo> {
field_ref!((*self), a)
.as_pointer()
.write(field!(_a0, a).with_mut(std::mem::take));
field_ref!((*self), b)
.as_pointer()
.write(field!(_a0, b).with_mut(std::mem::take));
return (*self).clone();
}
}
pub fn __cpp2rust_init_globals() {}
52 changes: 52 additions & 0 deletions tests/unit/out/unsafe/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,29 @@ pub struct S {
pub x: i32,
}
pub unsafe fn copy_0(mut copy_vector: Vec<i32>) {}
#[repr(C)]
#[derive(Clone, VaArg, FnPtrArg, MoveCtorUnsafe, MoveAssignUnsafe, Default)]
pub struct Foo {
pub a: Vec<i32>,
pub b: Vec<i32>,
}
impl Foo {
pub unsafe fn move_from(_a0: *mut Foo) -> Self {
let mut this = Self {
a: std::mem::take(&mut (*_a0).a),
b: std::mem::take(&mut (*_a0).b),
};
this
}
pub unsafe fn move_assign(&mut self, _a0: *mut Foo) -> *mut Foo {
self.a = std::mem::take(&mut (*_a0).a);
self.b = std::mem::take(&mut (*_a0).b);
return &mut (*(self as *mut Foo));
}
}
pub unsafe fn exchange_1(x: *mut Vec<i32>, y: *mut Vec<i32>) {
std::mem::swap(&mut (*x), &mut (*y));
}
pub fn main() {
unsafe {
__cpp2rust_init_globals();
Expand Down Expand Up @@ -298,6 +321,35 @@ unsafe fn main_0() -> i32 {
};
(*(*(*v9[(0_usize)]))).x = 2;
assert!(((a.x) == (2)));
let mut v10: Vec<i32> = Vec::from([1, 2, 3]);
let mut v11: Vec<i32> = Vec::from([4]);
std::mem::swap(&mut v10, &mut v11);
assert!(
((((v10.len()) == (1_usize)) && ((v10[(0_usize)]) == (4))) && ((v11.len()) == (3_usize)))
&& ((v11[(2_usize)]) == (3))
);
let mut foo: Foo = <Foo>::default();
{
let __a1 = 5;
foo.a.push(__a1)
};
{
let __a1 = 6;
foo.b.push(__a1)
};
{
let __a1 = 7;
foo.b.push(__a1)
};
std::mem::swap(&mut foo.a, &mut foo.b);
assert!(
(((foo.a.len()) == (2_usize)) && ((foo.a[(1_usize)]) == (7)))
&& ((foo.b[(0_usize)]) == (5))
);
(unsafe { exchange_1(&mut foo.a, &mut v10) });
assert!(
(((foo.a.len()) == (1_usize)) && ((foo.a[(0_usize)]) == (4))) && ((v10[(1_usize)]) == (7))
);
return 0;
}
pub unsafe fn __cpp2rust_init_globals() {}
20 changes: 20 additions & 0 deletions tests/unit/vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ struct S {

void copy(std::vector<int> copy_vector) {}

struct Foo {
std::vector<int> a;
std::vector<int> b;
};

static void exchange(std::vector<int> &x, std::vector<int> &y) { x.swap(y); }

int main() {
std::vector<int> v1;
assert(v1.size() == 0);
Expand Down Expand Up @@ -157,5 +164,18 @@ int main() {
v9.push_back(&ppa);
(**v9[0])->x = 2;
assert(a.x == 2);

std::vector<int> v10 = {1, 2, 3};
std::vector<int> v11 = {4};
v10.swap(v11);
assert(v10.size() == 1 && v10[0] == 4 && v11.size() == 3 && v11[2] == 3);
Foo foo;
foo.a.push_back(5);
foo.b.push_back(6);
foo.b.push_back(7);
foo.a.swap(foo.b);
assert(foo.a.size() == 2 && foo.a[1] == 7 && foo.b[0] == 5);
exchange(foo.a, v10);
assert(foo.a.size() == 1 && foo.a[0] == 4 && v10[1] == 7);
return 0;
}
Loading