Skip to content

Commit 09ee4cd

Browse files
committed
Use std::map::iterator
1 parent 78b9032 commit 09ee4cd

3 files changed

Lines changed: 32 additions & 91 deletions

File tree

tests/unit/out/refcount/redundant_copy_in_conversion.rs

Lines changed: 19 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,54 +6,27 @@ use std::io::prelude::*;
66
use std::io::{Read, Seek, Write};
77
use std::os::fd::AsFd;
88
use std::rc::{Rc, Weak};
9-
#[derive(Default)]
10-
pub struct iter {
11-
pub p: Value<Ptr<i32>>,
12-
}
13-
impl Clone for iter {
14-
fn clone(&self) -> Self {
15-
let mut this = Self {
16-
p: Rc::new(RefCell::new((*self.p.borrow()).clone())),
17-
};
18-
this
19-
}
20-
}
21-
impl ByteRepr for iter {}
22-
#[derive(Default)]
23-
pub struct const_iter {
24-
pub p: Value<Ptr<i32>>,
25-
}
26-
impl const_iter {
27-
pub fn const_iter(o: Ptr<iter>) -> Self {
28-
let mut this = Self {
29-
p: Rc::new(RefCell::new((*(*o.upgrade().deref()).p.borrow()).clone())),
30-
};
31-
this
32-
}
33-
}
34-
impl Clone for const_iter {
35-
fn clone(&self) -> Self {
36-
let mut this = Self {
37-
p: Rc::new(RefCell::new(Ptr::<i32>::null())),
38-
};
39-
this
40-
}
41-
}
42-
impl ByteRepr for const_iter {}
43-
pub fn sink_0(i: const_iter) {
44-
let i: Value<const_iter> = Rc::new(RefCell::new(i));
45-
}
469
pub fn main() {
4710
std::process::exit(main_0());
4811
}
4912
fn main_0() -> i32 {
50-
let buf: Value<Box<[i32]>> = Rc::new(RefCell::new(Box::new([0, 0])));
51-
let it: Value<iter> = Rc::new(RefCell::new(iter {
52-
p: Rc::new(RefCell::new((buf.as_pointer() as Ptr<i32>))),
53-
}));
54-
({
55-
let _i: const_iter = const_iter::const_iter(it.as_pointer());
56-
sink_0(_i)
57-
});
58-
return 0;
13+
let m: Value<BTreeMap<i32, Value<i32>>> = Rc::new(RefCell::new(BTreeMap::new()));
14+
(m.as_pointer() as Ptr<BTreeMap<i32, Value<i32>>>)
15+
.with_mut(|__v: &mut BTreeMap<i32, Value<i32>>| {
16+
__v.entry(0.clone())
17+
.or_insert_with(|| Rc::new(RefCell::new(<i32>::default())))
18+
.as_pointer()
19+
})
20+
.write(1);
21+
let end: Value<RefcountMapIter<i32, i32>> = Rc::new(RefCell::new(RefcountMapIter::end(
22+
(m.as_pointer() as Ptr<BTreeMap<i32, Value<i32>>>),
23+
)));
24+
let const_it: Value<RefcountMapIter<i32, i32>> = Rc::new(RefCell::new(
25+
RefcountMapIter::find_key((m.as_pointer() as Ptr<BTreeMap<i32, Value<i32>>>), &0),
26+
));
27+
return if (*const_it.borrow()) == (*end.borrow()) {
28+
0
29+
} else {
30+
1
31+
};
5932
}

tests/unit/out/unsafe/redundant_copy_in_conversion.rs

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,17 @@ use std::collections::BTreeMap;
66
use std::io::{Read, Seek, Write};
77
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
88
use std::rc::Rc;
9-
#[repr(C)]
10-
#[derive(Copy, Clone, Default)]
11-
pub struct iter {
12-
pub p: *mut i32,
13-
}
14-
#[repr(C)]
15-
#[derive(Copy, Clone, Default)]
16-
pub struct const_iter {
17-
pub p: *const i32,
18-
}
19-
impl const_iter {
20-
pub unsafe fn const_iter(o: *const iter) -> Self {
21-
let mut this = Self {
22-
p: (*o).p.cast_const(),
23-
};
24-
this
25-
}
26-
}
27-
pub unsafe fn sink_0(mut i: const_iter) {}
289
pub fn main() {
2910
unsafe {
3011
std::process::exit(main_0() as i32);
3112
}
3213
}
3314
unsafe fn main_0() -> i32 {
34-
let mut buf: [i32; 2] = [0, 0];
35-
let mut it: iter = iter {
36-
p: buf.as_mut_ptr(),
37-
};
38-
(unsafe {
39-
let _i: const_iter = const_iter::const_iter(&it as *const iter);
40-
sink_0(_i)
41-
});
42-
return 0;
15+
let mut m: BTreeMap<i32, Box<i32>> = BTreeMap::new();
16+
(*m.entry(0).or_default().as_mut()) = 1;
17+
let mut end: UnsafeMapIterator<i32, i32> =
18+
UnsafeMapIterator::end(&m as *const BTreeMap<i32, Box<i32>>);
19+
let mut const_it: UnsafeMapIterator<i32, i32> =
20+
UnsafeMapIterator::find_key(&m as *const BTreeMap<i32, Box<i32>>, &0);
21+
return if const_it == end { 0 } else { 1 };
4322
}
Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,9 @@
1-
struct iter {
2-
using iterator_category = int;
3-
int *p;
4-
};
5-
6-
struct const_iter {
7-
using iterator_category = int;
8-
const int *p;
9-
const_iter(const iter &o) : p(o.p) {}
10-
const_iter(const const_iter &) = default;
11-
};
12-
13-
static void sink(const_iter i) {}
1+
#include <map>
142

153
int main() {
16-
int buf[2] = {0, 0};
17-
iter it{buf};
18-
sink(it);
19-
return 0;
4+
std::map<int, int> m;
5+
m[0] = 1;
6+
std::map<int, int>::iterator end = m.end();
7+
std::map<int, int>::const_iterator const_it = m.find(0);
8+
return const_it == end ? 0 : 1;
209
}

0 commit comments

Comments
 (0)