Skip to content

Commit 9b76e36

Browse files
committed
Add test with a moved struct
1 parent 085268a commit 9b76e36

3 files changed

Lines changed: 107 additions & 0 deletions

File tree

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
extern crate libcc2rs;
2+
use libcc2rs::*;
3+
use std::cell::RefCell;
4+
use std::collections::BTreeMap;
5+
use std::io::prelude::*;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::AsFd;
8+
use std::rc::{Rc, Weak};
9+
#[derive(Default)]
10+
pub struct S {
11+
pub a: Value<i32>,
12+
pub b: Value<i32>,
13+
}
14+
impl S {
15+
pub fn S(a: i32, b: i32) -> Self {
16+
let a: Value<i32> = Rc::new(RefCell::new(a));
17+
let b: Value<i32> = Rc::new(RefCell::new(b));
18+
let mut this = Self {
19+
a: Rc::new(RefCell::new((*a.borrow()))),
20+
b: Rc::new(RefCell::new((*b.borrow()))),
21+
};
22+
this
23+
}
24+
}
25+
impl Clone for S {
26+
fn clone(&self) -> Self {
27+
let mut this = Self {
28+
a: Rc::new(RefCell::new((*self.a.borrow()))),
29+
b: Rc::new(RefCell::new((*self.b.borrow()))),
30+
};
31+
this
32+
}
33+
}
34+
impl ByteRepr for S {
35+
fn byte_size() -> usize {
36+
8
37+
}
38+
fn to_bytes(&self, buf: &mut [u8]) {
39+
(*self.a.borrow()).to_bytes(&mut buf[0..4]);
40+
(*self.b.borrow()).to_bytes(&mut buf[4..8]);
41+
}
42+
fn from_bytes(buf: &[u8]) -> Self {
43+
Self {
44+
a: Rc::new(RefCell::new(<i32>::from_bytes(&buf[0..4]))),
45+
b: Rc::new(RefCell::new(<i32>::from_bytes(&buf[4..8]))),
46+
}
47+
}
48+
}
49+
pub fn main() {
50+
std::process::exit(main_0());
51+
}
52+
fn main_0() -> i32 {
53+
let s1: Value<S> = Rc::new(RefCell::new(S::S({ 1 }, { 2 })));
54+
let s2: Ptr<S> = s1.as_pointer();
55+
assert!(((*(*s2.upgrade().deref()).a.borrow()) == 1));
56+
assert!(((*(*s2.upgrade().deref()).b.borrow()) == 2));
57+
return 0;
58+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
extern crate libc;
2+
use libc::*;
3+
extern crate libcc2rs;
4+
use libcc2rs::*;
5+
use std::collections::BTreeMap;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
8+
use std::rc::Rc;
9+
#[repr(C)]
10+
#[derive(Copy, Clone, Default)]
11+
pub struct S {
12+
pub a: i32,
13+
pub b: i32,
14+
}
15+
impl S {
16+
pub unsafe fn S(mut a: i32, mut b: i32) -> Self {
17+
let mut this = Self { a: a, b: b };
18+
this
19+
}
20+
}
21+
pub fn main() {
22+
unsafe {
23+
std::process::exit(main_0() as i32);
24+
}
25+
}
26+
unsafe fn main_0() -> i32 {
27+
let mut s1: S = S::S({ 1 }, { 2 });
28+
let s2: *mut S = &mut s1;
29+
assert!((((*s2).a) == (1)));
30+
assert!((((*s2).b) == (2)));
31+
return 0;
32+
}

‎tests/unit/rvalue_struct.cpp‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <cassert>
2+
#include <utility>
3+
4+
struct S {
5+
int a;
6+
int b;
7+
8+
S(int a, int b) : a(a), b(b) {}
9+
};
10+
11+
int main() {
12+
S s1(1, 2);
13+
S &&s2 = std::move(s1);
14+
assert(s2.a == 1);
15+
assert(s2.b == 2);
16+
return 0;
17+
}

0 commit comments

Comments
 (0)