Skip to content

Commit 006d714

Browse files
committed
Receive &Ptr instead of Ptr in PtrValueIter::new
1 parent 64e7a61 commit 006d714

10 files changed

Lines changed: 77 additions & 75 deletions

File tree

libcc2rs/src/rc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -639,8 +639,8 @@ pub struct PtrValueIter<T> {
639639
}
640640

641641
impl<T> PtrValueIter<T> {
642-
pub fn new(ptr: Ptr<T>, n: usize) -> Self {
643-
Self { ptr, n }
642+
pub fn new(ptr: &Ptr<T>, n: usize) -> Self {
643+
Self { ptr: ptr.clone() , n }
644644
}
645645
}
646646

rules/algorithm/ir_refcount.json

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@
127127
}
128128
},
129129
{
130-
"text": ";\n let mut iter = PtrValueIter::new("
130+
"text": ";\n let mut iter = PtrValueIter::new(&"
131131
},
132132
{
133133
"placeholder": {
@@ -975,7 +975,7 @@
975975
}
976976
},
977977
{
978-
"text": ";\n for value in PtrValueIter::new("
978+
"text": ";\n for value in PtrValueIter::new(&"
979979
},
980980
{
981981
"placeholder": {
@@ -1430,23 +1430,12 @@
14301430
"method_call": {
14311431
"receiver": [
14321432
{
1433-
"text": "PtrValueIter::new("
1433+
"text": "PtrValueIter::new(&"
14341434
},
14351435
{
1436-
"method_call": {
1437-
"receiver": [
1438-
{
1439-
"placeholder": {
1440-
"arg": 0,
1441-
"access": "read"
1442-
}
1443-
}
1444-
],
1445-
"body": [
1446-
{
1447-
"text": ".clone()"
1448-
}
1449-
]
1436+
"placeholder": {
1437+
"arg": 0,
1438+
"access": "read"
14501439
}
14511440
},
14521441
{

rules/algorithm/tgt_refcount.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ fn f2<T1: Clone + PartialOrd + ByteRepr, T2: Clone + From<T1> + ByteRepr>(
1717
) -> Ptr<T2> {
1818
let count = a1.get_offset() - a0.get_offset();
1919
let mut outptr = a2.clone();
20-
for value in PtrValueIter::new(a0, count) {
20+
for value in PtrValueIter::new(&a0, count) {
2121
outptr.write(value.into());
2222
outptr += 1;
2323
}
@@ -52,7 +52,7 @@ where
5252

5353
fn f8<T1: PartialOrd + Clone + ByteRepr>(a0: Ptr<T1>, a1: Ptr<T1>) -> Ptr<T1> {
5454
let count = a1.get_offset() - a0.get_offset();
55-
let max_index = PtrValueIter::new(a0.clone(), count)
55+
let max_index = PtrValueIter::new(&a0, count)
5656
.enumerate()
5757
.max_by(|(_, val_a), (_, val_b)| {
5858
val_a
@@ -76,7 +76,7 @@ fn f10<T1: PartialEq + Clone + ByteRepr>(a0: Ptr<T1>, a1: Ptr<T1>) -> Ptr<T1> {
7676
a1
7777
} else {
7878
let mut write_ptr = a0.clone();
79-
let mut iter = PtrValueIter::new(a0, count);
79+
let mut iter = PtrValueIter::new(&a0, count);
8080
let mut last_unique = iter.next().unwrap();
8181

8282
// the first unique value is already in place

rules/vector/ir_refcount.json

Lines changed: 21 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,7 +1071,7 @@
10711071
"method_call": {
10721072
"receiver": [
10731073
{
1074-
"text": "PtrValueIter::new("
1074+
"text": "PtrValueIter::new(&"
10751075
},
10761076
{
10771077
"placeholder": {
@@ -1164,23 +1164,12 @@
11641164
"method_call": {
11651165
"receiver": [
11661166
{
1167-
"text": "PtrValueIter::new("
1167+
"text": "PtrValueIter::new(&"
11681168
},
11691169
{
1170-
"method_call": {
1171-
"receiver": [
1172-
{
1173-
"placeholder": {
1174-
"arg": 0,
1175-
"access": "read"
1176-
}
1177-
}
1178-
],
1179-
"body": [
1180-
{
1181-
"text": ".clone()"
1182-
}
1183-
]
1170+
"placeholder": {
1171+
"arg": 0,
1172+
"access": "read"
11841173
}
11851174
},
11861175
{
@@ -1318,27 +1307,7 @@
13181307
"f42": {
13191308
"body": [
13201309
{
1321-
"text": "let __a0 = "
1322-
},
1323-
{
1324-
"method_call": {
1325-
"receiver": [
1326-
{
1327-
"placeholder": {
1328-
"arg": 0,
1329-
"access": "read"
1330-
}
1331-
}
1332-
],
1333-
"body": [
1334-
{
1335-
"text": ".clone()"
1336-
}
1337-
]
1338-
}
1339-
},
1340-
{
1341-
"text": ";\n let __count = "
1310+
"text": "let __count = "
13421311
},
13431312
{
13441313
"method_call": {
@@ -1364,7 +1333,10 @@
13641333
"method_call": {
13651334
"receiver": [
13661335
{
1367-
"text": "__a0"
1336+
"placeholder": {
1337+
"arg": 0,
1338+
"access": "read"
1339+
}
13681340
}
13691341
],
13701342
"body": [
@@ -1390,7 +1362,16 @@
13901362
"method_call": {
13911363
"receiver": [
13921364
{
1393-
"text": "PtrValueIter::new(__a0, __count)"
1365+
"text": "PtrValueIter::new(&"
1366+
},
1367+
{
1368+
"placeholder": {
1369+
"arg": 0,
1370+
"access": "read"
1371+
}
1372+
},
1373+
{
1374+
"text": ", __count)"
13941375
}
13951376
],
13961377
"body": [
@@ -1809,7 +1790,7 @@
18091790
"method_call": {
18101791
"receiver": [
18111792
{
1812-
"text": "PtrValueIter::new("
1793+
"text": "PtrValueIter::new(&"
18131794
},
18141795
{
18151796
"placeholder": {

rules/vector/tgt_refcount.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,12 @@ fn f34<T1>(a0: &mut Ptr<T1>) -> Ptr<T1> {
114114

115115
fn f35<T1: Clone + ByteRepr>(a0: Ptr<T1>, a1: Ptr<T1>) -> Vec<T1> {
116116
let __count = a1.get_offset() - a0.get_offset();
117-
PtrValueIter::new(a0, __count).collect::<Vec<_>>()
117+
PtrValueIter::new(&a0, __count).collect::<Vec<_>>()
118118
}
119119

120120
fn f37<T1: TryFrom<T2>, T2: Clone + ByteRepr>(a0: Ptr<T2>, a1: Ptr<T2>) -> Vec<T1> {
121121
let __count = a1.get_offset() - a0.get_offset();
122-
PtrValueIter::new(a0.clone(), __count)
122+
PtrValueIter::new(&a0, __count)
123123
.map(|item| T1::try_from(item).ok().unwrap())
124124
.collect::<Vec<_>>()
125125
}
@@ -133,9 +133,8 @@ fn f41<T1>(a0: Ptr<T1>) -> Ptr<T1> {
133133
}
134134

135135
fn f42(a0: Ptr<u8>, a1: Ptr<u8>) -> Ptr<u8> {
136-
let __a0 = a0.clone();
137-
let __count = a1.get_offset() - __a0.get_offset();
138-
let max_index = PtrValueIter::new(__a0, __count)
136+
let __count = a1.get_offset() - a0.get_offset();
137+
let max_index = PtrValueIter::new(&a0, __count)
139138
.enumerate()
140139
.max_by_key(|&(_, val)| val)
141140
.map(|(idx, _)| idx)
@@ -176,7 +175,7 @@ fn f53<T1: Clone + ByteRepr>(
176175
) -> Ptr<Vec<T1>> {
177176
let start_idx = a1.get_offset();
178177
let count = a3.get_offset() - a2.get_offset();
179-
let temp_vec: Vec<T1> = PtrValueIter::new(a2, count).collect();
178+
let temp_vec: Vec<T1> = PtrValueIter::new(&a2, count).collect();
180179
a0.with_mut(|v: &mut Vec<T1>| {
181180
v.splice(start_idx..start_idx, temp_vec);
182181
});

tests/unit/out/refcount/push_emplace_back.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ pub fn push_local_from_field_1(jpg: Ptr<JPEGData>, cond: bool) {
8484
.offset((3) as isize)
8585
.get_offset()
8686
- (head.as_pointer() as Ptr<u8>).get_offset();
87-
PtrValueIter::new((head.as_pointer() as Ptr<u8>).clone(), __count)
87+
PtrValueIter::new(&(head.as_pointer() as Ptr<u8>), __count)
8888
.map(|item| u8::try_from(item).ok().unwrap())
8989
.collect::<Vec<_>>()
9090
}
@@ -125,7 +125,7 @@ pub fn emplace_local_from_field_4(jpg: Ptr<JPEGData>, cond: bool) {
125125
.offset((3) as isize)
126126
.get_offset()
127127
- (head.as_pointer() as Ptr<u8>).get_offset();
128-
PtrValueIter::new((head.as_pointer() as Ptr<u8>).clone(), __count)
128+
PtrValueIter::new(&(head.as_pointer() as Ptr<u8>), __count)
129129
.map(|item| u8::try_from(item).ok().unwrap())
130130
.collect::<Vec<_>>()
131131
})))

tests/unit/out/refcount/stdcopy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fn main_0() -> i32 {
2020
.get_offset()
2121
- (input.as_pointer() as Ptr<i32>).get_offset();
2222
let mut outptr = (output.as_pointer() as Ptr<i32>).clone();
23-
for value in PtrValueIter::new((input.as_pointer() as Ptr<i32>), count) {
23+
for value in PtrValueIter::new(&(input.as_pointer() as Ptr<i32>), count) {
2424
outptr.write(value.into());
2525
outptr += 1;
2626
}

tests/unit/out/refcount/vector_iter_range_ctor.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fn main_0() -> i32 {
1616
.offset((3) as isize)
1717
.get_offset()
1818
- (src.as_pointer() as Ptr<u32>).get_offset();
19-
PtrValueIter::new((src.as_pointer() as Ptr<u32>).clone(), __count)
19+
PtrValueIter::new(&(src.as_pointer() as Ptr<u32>), __count)
2020
.map(|item| u32::try_from(item).ok().unwrap())
2121
.collect::<Vec<_>>()
2222
}));
@@ -31,7 +31,7 @@ fn main_0() -> i32 {
3131
.offset((3) as isize)
3232
.get_offset()
3333
- (src.as_pointer() as Ptr<u32>).get_offset();
34-
PtrValueIter::new((src.as_pointer() as Ptr<u32>).clone(), __count)
34+
PtrValueIter::new(&(src.as_pointer() as Ptr<u32>), __count)
3535
.map(|item| u64::try_from(item).ok().unwrap())
3636
.collect::<Vec<_>>()
3737
}));
@@ -46,7 +46,7 @@ fn main_0() -> i32 {
4646
.offset((3) as isize)
4747
.get_offset()
4848
- (src.as_pointer() as Ptr<u32>).get_offset();
49-
PtrValueIter::new((src.as_pointer() as Ptr<u32>).clone(), __count)
49+
PtrValueIter::new(&(src.as_pointer() as Ptr<u32>), __count)
5050
.map(|item| i32::try_from(item).ok().unwrap())
5151
.collect::<Vec<_>>()
5252
}));
@@ -60,13 +60,29 @@ fn main_0() -> i32 {
6060
let v4: Value<Vec<u32>> = Rc::new(RefCell::new({
6161
let __count = (src1.as_pointer() as Ptr<u32>).to_end().get_offset()
6262
- (src1.as_pointer() as Ptr<u32>).get_offset();
63-
PtrValueIter::new((src1.as_pointer() as Ptr<u32>), __count).collect::<Vec<_>>()
63+
PtrValueIter::new(&(src1.as_pointer() as Ptr<u32>), __count).collect::<Vec<_>>()
6464
}));
6565
assert!(((*v4.borrow()).len() as u64 == 3_u64));
6666
assert!(
6767
((((v4.as_pointer() as Ptr<u32>).offset(0_u64 as isize).read()) == 1_u32)
6868
&& (((v4.as_pointer() as Ptr<u32>).offset(1_u64 as isize).read()) == 2_u32))
6969
&& (((v4.as_pointer() as Ptr<u32>).offset(2_u64 as isize).read()) == 3_u32)
7070
);
71+
let buf: Value<Box<[u8]>> =
72+
Rc::new(RefCell::new(Box::new([10_u8, 20_u8, 30_u8, 40_u8, 50_u8])));
73+
let start: Value<Ptr<u8>> = Rc::new(RefCell::new((buf.as_pointer() as Ptr<u8>)));
74+
let len: Value<u64> = Rc::new(RefCell::new(5_u64));
75+
let v5: Value<Vec<u8>> = Rc::new(RefCell::new({
76+
let __count = (*start.borrow())
77+
.offset((*len.borrow()) as isize)
78+
.get_offset()
79+
- (*start.borrow()).get_offset();
80+
PtrValueIter::new(&(*start.borrow()), __count).collect::<Vec<_>>()
81+
}));
82+
assert!(((*v5.borrow()).len() as u64 == 5_u64));
83+
assert!(
84+
((((v5.as_pointer() as Ptr<u8>).offset(0_u64 as isize).read()) as i32) == 10)
85+
&& ((((v5.as_pointer() as Ptr<u8>).offset(4_u64 as isize).read()) as i32) == 50)
86+
);
7187
return 0;
7288
}

tests/unit/out/unsafe/vector_iter_range_ctor.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,15 @@ unsafe fn main_0() -> i32 {
6060
(((v4[(0_u64) as usize]) == (1_u32)) && ((v4[(1_u64) as usize]) == (2_u32)))
6161
&& ((v4[(2_u64) as usize]) == (3_u32))
6262
);
63+
let mut buf: [u8; 5] = [10_u8, 20_u8, 30_u8, 40_u8, 50_u8];
64+
let mut start: *const u8 = buf.as_mut_ptr().cast_const();
65+
let mut len: u64 = 5_u64;
66+
let mut v5: Vec<u8> = core::slice::from_raw_parts(
67+
start,
68+
(start.offset((len) as isize)).offset_from(start) as usize,
69+
)
70+
.to_vec();
71+
assert!(((v5.len() as u64) == (5_u64)));
72+
assert!(((v5[(0_u64) as usize] as i32) == (10)) && ((v5[(4_u64) as usize] as i32) == (50)));
6373
return 0;
6474
}

tests/unit/vector_iter_range_ctor.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,12 @@ int main() {
2626
assert(v4.size() == 3);
2727
assert(v4[0] == 1 && v4[1] == 2 && v4[2] == 3);
2828

29+
uint8_t buf[5] = {10, 20, 30, 40, 50};
30+
const uint8_t *start = buf;
31+
size_t len = 5;
32+
std::vector<uint8_t> v5(start, start + len);
33+
assert(v5.size() == 5);
34+
assert(v5[0] == 10 && v5[4] == 50);
35+
2936
return 0;
3037
}

0 commit comments

Comments
 (0)