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
56 changes: 28 additions & 28 deletions rules/map/shim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,8 @@ impl<K: CppLess + 'static, V> Map<K, Entry<K, V>> {
.as_pointer()
}

fn insert_after(&mut self, prev: Value<MapNode<Entry<K, V>>>, key: K) -> Ptr<V>
where
V: Default,
{
let entry = (
Rc::new(RefCell::new(key)),
Rc::new(RefCell::new(V::default())),
);
fn insert_after(&mut self, prev: Value<MapNode<Entry<K, V>>>, key: K, value: V) -> Ptr<V> {
let entry = (Rc::new(RefCell::new(key)), Rc::new(RefCell::new(value)));
Self::mapped(&self.link_after(prev, entry))
}

Expand Down Expand Up @@ -231,26 +225,29 @@ impl<K: CppLess + 'static, V> Map<K, Entry<K, V>> {
// The functions that modify a map take a pointer to it, and borrow it after
// their arguments are evaluated, which may access the map.
impl<K: CppLess + 'static, V: 'static> Map<K, Entry<K, V>> {
// operator[] with a key that is copied if inserted.
pub fn subscript(&mut self, key: Ptr<K>) -> Ptr<V>
// operator[] with a key that is copied, and a value made by new_value, if
// inserted.
pub fn subscript(&mut self, key: Ptr<K>, new_value: impl FnOnce() -> V) -> Ptr<V>
where
K: Clone + ByteRepr,
V: Default,
{
match self.search(&key) {
Ok(node) => Self::mapped(&node),
Err(prev) => self.insert_after(prev, key.read()),
Err(prev) => self.insert_after(prev, key.read(), new_value()),
}
}

// operator[] with a key that is moved, by new_key, if inserted.
pub fn subscript_move(&mut self, key: Ptr<K>, new_key: impl FnOnce(Ptr<K>) -> K) -> Ptr<V>
where
V: Default,
{
// operator[] with a key that is moved, by new_key, and a value made by
// new_value, if inserted.
pub fn subscript_move(
&mut self,
key: Ptr<K>,
new_key: impl FnOnce(Ptr<K>) -> K,
new_value: impl FnOnce() -> V,
) -> Ptr<V> {
match self.search(&key) {
Ok(node) => Self::mapped(&node),
Err(prev) => self.insert_after(prev, new_key(key)),
Err(prev) => self.insert_after(prev, new_key(key), new_value()),
}
}

Expand Down Expand Up @@ -486,29 +483,32 @@ impl<K: Ord, V> UnsafeMap<K, (K, V)> {
unsafe { &raw mut (*UnsafeMapNode::value(node)).1 }
}

// operator[] with a key that is copied if inserted.
pub fn subscript(&mut self, key: &K) -> *mut V
// operator[] with a key that is copied, and a value made by new_value, if
// inserted.
pub fn subscript(&mut self, key: &K, new_value: impl FnOnce() -> V) -> *mut V
where
K: Clone,
V: Default,
{
match self.search(key) {
Ok(node) => Self::mapped(node),
Err(prev) => Self::mapped(self.link_after(prev, (key.clone(), V::default()))),
Err(prev) => Self::mapped(self.link_after(prev, (key.clone(), new_value()))),
}
}

/// operator[] with a key that is moved, by new_key, if inserted.
/// operator[] with a key that is moved, by new_key, and a value made by
/// new_value, if inserted.
///
/// # Safety
/// `key` must point to a live key.
pub unsafe fn subscript_move(&mut self, key: *mut K, new_key: impl FnOnce(*mut K) -> K) -> *mut V
where
V: Default,
{
pub unsafe fn subscript_move(
&mut self,
key: *mut K,
new_key: impl FnOnce(*mut K) -> K,
new_value: impl FnOnce() -> V,
) -> *mut V {
match self.search(unsafe { &*key }) {
Ok(node) => Self::mapped(node),
Err(prev) => Self::mapped(self.link_after(prev, (new_key(key), V::default()))),
Err(prev) => Self::mapped(self.link_after(prev, (new_key(key), new_value()))),
}
}

Expand Down
4 changes: 2 additions & 2 deletions rules/map/tgt_refcount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn f1<T1: CppLess + Clone + ByteRepr + 'static, T2: Default + 'static>(
a0: &mut Map<T1, (Value<T1>, Value<T2>)>,
a1: Ptr<T1>,
) -> Ptr<T2> {
a0.subscript(a1)
a0.subscript(a1, || T2::default())
}

fn f2<T1, T2>(a0: Map<T1, (Value<T1>, Value<T2>)>) -> usize {
Expand Down Expand Up @@ -43,7 +43,7 @@ fn f8<T1: CppLess + 'static, T2: Default + 'static>(
a0: &mut Map<T1, (Value<T1>, Value<T2>)>,
a1: Ptr<T1>,
) -> Ptr<T2> {
a0.subscript_move(a1, |__k| cpp_move!(__k))
a0.subscript_move(a1, |__k| cpp_move!(__k), || T2::default())
}

fn f9<T1: 'static, T2: 'static>(a0: Ptr<Map<T1, (Value<T1>, Value<T2>)>>) -> BidirectionalIterator<(Value<T1>, Value<T2>)> {
Expand Down
4 changes: 2 additions & 2 deletions rules/map/tgt_unsafe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn t6<T1, T2>() -> T2 {
}

unsafe fn f1<T1: Ord + Clone, T2: Default>(a0: &mut UnsafeMap<T1, (T1, T2)>, a1: T1) -> &mut T2 {
&mut (*a0.subscript(&a1))
&mut (*a0.subscript(&a1, || T2::default()))
}

fn f2<T1, T2>(a0: UnsafeMap<T1, (T1, T2)>) -> usize {
Expand All @@ -44,7 +44,7 @@ unsafe fn f7<T1: Ord, T2>(a0: &mut UnsafeMap<T1, (T1, T2)>, a1: T1) -> *const T2
}

unsafe fn f8<T1: Ord, T2: Default>(a0: &mut UnsafeMap<T1, (T1, T2)>, a1: *mut T1) -> &mut T2 {
&mut (*a0.subscript_move(a1, |__k| cpp_move_unsafe!(__k)))
&mut (*a0.subscript_move(a1, |__k| cpp_move_unsafe!(__k), || T2::default()))
}

fn f9<T1, T2>(a0: UnsafeMap<T1, (T1, T2)>) -> UnsafeBidirectionalIterator<(T1, T2)> {
Expand Down
34 changes: 34 additions & 0 deletions tests/std/containers/associative/map/map.access.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// ADDITIONAL_COMPILE_FLAGS: -std=c++23
#include <cassert>
#include <map>
#include <string>

// N4950 [map.access]/1: operator[](const key_type& x) is equivalent to
// return try_emplace(x).first->second, which value-initializes the mapped
// value of a key that is not in the map.
namespace p1_subscript_lvalue_key_default_string {
void test() {
std::map<int, std::string> m;
int key = 1;
assert(m[key].size() == 0);
assert(*m[key].c_str() == '\0');
assert(m.size() == 1);
}
} // namespace p1_subscript_lvalue_key_default_string

// N4950 [map.access]/2: operator[](key_type&& x) is equivalent to
// return try_emplace(std::move(x)).first->second.
namespace p2_subscript_rvalue_key_default_string {
void test() {
std::map<int, std::string> m;
assert(m[1].size() == 0);
assert(*m[1].c_str() == '\0');
assert(m.size() == 1);
}
} // namespace p2_subscript_rvalue_key_default_string

int main() {
p1_subscript_lvalue_key_default_string::test();
p2_subscript_rvalue_key_default_string::test();
return 0;
}
65 changes: 65 additions & 0 deletions tests/std/containers/associative/map/out/refcount/map.access.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
extern crate libcc2rs;
use libcc2rs::*;
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::io::prelude::*;
use std::io::{Read, Seek, Write};
use std::os::fd::AsFd;
use std::rc::{Rc, Weak};
pub fn test_0() {
let m: Value<Map<i32, (Value<i32>, Value<Vec<i8>>)>> = Rc::new(RefCell::new(Map::new()));
let key: Value<i32> = Rc::new(RefCell::new(1));
assert!(
({
(((*({ (*m.borrow_mut()).subscript(key.as_pointer(), || vec![0]) })
.upgrade()
.deref())
.len()
- 1)
== 0_usize)
})
);
assert!(
(((({ (*m.borrow_mut()).subscript(key.as_pointer(), || vec![0]) })
.decay()
.read()) as i32)
== (('\0' as i8) as i32))
);
assert!(({ ((*m.borrow()).len() == 1_usize) }));
}
pub fn test_1() {
let m: Value<Map<i32, (Value<i32>, Value<Vec<i8>>)>> = Rc::new(RefCell::new(Map::new()));
assert!(
(((*match (Rc::new(RefCell::new(1)).as_pointer(),) {
(__tmp_0,) => {
({ (*m.borrow_mut()).subscript_move(__tmp_0, |__k| cpp_move!(__k), || vec![0]) })
}
}
.upgrade()
.deref())
.len()
- 1)
== 0_usize)
);
assert!(
(((match (Rc::new(RefCell::new(1)).as_pointer(),) {
(__tmp_1,) => {
({ (*m.borrow_mut()).subscript_move(__tmp_1, |__k| cpp_move!(__k), || vec![0]) })
}
}
.decay()
.read()) as i32)
== (('\0' as i8) as i32))
);
assert!(({ ((*m.borrow()).len() == 1_usize) }));
}
pub fn main() {
__cpp2rust_init_globals();
libcc2rs::cpp2rust_exit(main_0());
}
fn main_0() -> i32 {
({ test_0() });
({ test_1() });
return 0;
}
pub fn __cpp2rust_init_globals() {}
41 changes: 41 additions & 0 deletions tests/std/containers/associative/map/out/unsafe/map.access.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
extern crate libc;
use libc::*;
extern crate libcc2rs;
use libcc2rs::*;
use std::collections::BTreeMap;
use std::io::{Read, Seek, Write};
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
use std::rc::Rc;
pub unsafe fn test_0() {
let mut m: UnsafeMap<i32, (i32, Vec<libc::c_char>)> = UnsafeMap::new();
let mut key: i32 = 1;
assert!((((*m.subscript(&key, || vec![0])).len() - 1) == (0_usize)));
assert!(
(((*(*m.subscript(&key, || vec![0])).as_ptr()) as i32) == (('\0' as libc::c_char) as i32))
);
assert!(((m.len()) == (1_usize)));
}
pub unsafe fn test_1() {
let mut m: UnsafeMap<i32, (i32, Vec<libc::c_char>)> = UnsafeMap::new();
assert!(
(((*m.subscript_move(&mut 1, |__k| cpp_move_unsafe!(__k), || vec![0])).len() - 1)
== (0_usize))
);
assert!(
(((*(*m.subscript_move(&mut 1, |__k| cpp_move_unsafe!(__k), || vec![0])).as_ptr()) as i32)
== (('\0' as libc::c_char) as i32))
);
assert!(((m.len()) == (1_usize)));
}
pub fn main() {
unsafe {
__cpp2rust_init_globals();
libcc2rs::cpp2rust_exit(main_0() as i32);
}
}
unsafe fn main_0() -> i32 {
(unsafe { test_0() });
(unsafe { test_1() });
return 0;
}
pub unsafe fn __cpp2rust_init_globals() {}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ pub fn test_0() {
let m: Value<Map<i32, (Value<i32>, Value<i32>)>> = Rc::new(RefCell::new(Map::new()));
let n: Value<Map<i32, (Value<i32>, Value<i32>)>> = Rc::new(RefCell::new(Map::new()));
match (Rc::new(RefCell::new(1)).as_pointer(),) {
(__tmp_0,) => ({ (*m.borrow_mut()).subscript_move(__tmp_0, |__k| cpp_move!(__k)) }),
(__tmp_0,) => {
({ (*m.borrow_mut()).subscript_move(__tmp_0, |__k| cpp_move!(__k), || 0_i32) })
}
}
.write(2);
{
Expand All @@ -33,7 +35,7 @@ pub fn test_0() {
assert!(
((match (Rc::new(RefCell::new(1)).as_pointer(),) {
(__tmp_1,) => {
({ (*n.borrow_mut()).subscript_move(__tmp_1, |__k| cpp_move!(__k)) })
({ (*n.borrow_mut()).subscript_move(__tmp_1, |__k| cpp_move!(__k), || 0_i32) })
}
}
.read())
Expand All @@ -47,13 +49,15 @@ pub struct p18_rebind_to_container_node_Alloc_std_pair_const_int__int__ {}
pub fn test_1() {
let m: Value<Map<i32, (Value<i32>, Value<i32>)>> = Rc::new(RefCell::new(Map::new()));
match (Rc::new(RefCell::new(1)).as_pointer(),) {
(__tmp_2,) => ({ (*m.borrow_mut()).subscript_move(__tmp_2, |__k| cpp_move!(__k)) }),
(__tmp_2,) => {
({ (*m.borrow_mut()).subscript_move(__tmp_2, |__k| cpp_move!(__k), || 0_i32) })
}
}
.write(2);
assert!(
((match (Rc::new(RefCell::new(1)).as_pointer(),) {
(__tmp_3,) => {
({ (*m.borrow_mut()).subscript_move(__tmp_3, |__k| cpp_move!(__k)) })
({ (*m.borrow_mut()).subscript_move(__tmp_3, |__k| cpp_move!(__k), || 0_i32) })
}
}
.read())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@ impl Default for p16_rebind_member_Alloc_std_pair_const_int__int__ {
pub unsafe fn test_0() {
let mut m: UnsafeMap<i32, (i32, i32)> = UnsafeMap::new();
let mut n: UnsafeMap<i32, (i32, i32)> = UnsafeMap::new();
(*m.subscript_move(&mut 1, |__k| cpp_move_unsafe!(__k))) = 2;
(*m.subscript_move(&mut 1, |__k| cpp_move_unsafe!(__k), || 0_i32)) = 2;
{
n = std::mem::take(&mut m);
};
assert!(((*n.subscript_move(&mut 1, |__k| cpp_move_unsafe!(__k))) == (2)));
assert!(((*n.subscript_move(&mut 1, |__k| cpp_move_unsafe!(__k), || 0_i32)) == (2)));
assert!(((n.len()) == (1_usize)));
}
#[repr(C)]
#[derive(Copy, Clone, VaArg, FnPtrArg, Default)]
pub struct p18_rebind_to_container_node_Alloc_std_pair_const_int__int__ {}
pub unsafe fn test_1() {
let mut m: UnsafeMap<i32, (i32, i32)> = UnsafeMap::new();
(*m.subscript_move(&mut 1, |__k| cpp_move_unsafe!(__k))) = 2;
assert!(((*m.subscript_move(&mut 1, |__k| cpp_move_unsafe!(__k))) == (2)));
(*m.subscript_move(&mut 1, |__k| cpp_move_unsafe!(__k), || 0_i32)) = 2;
assert!(((*m.subscript_move(&mut 1, |__k| cpp_move_unsafe!(__k), || 0_i32)) == (2)));
assert!(((m.len()) == (1_usize)));
}
pub fn main() {
Expand Down
12 changes: 7 additions & 5 deletions tests/unit/out/refcount/clone_vs_move.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ fn main_0() -> i32 {
let i: Value<i32> = Rc::new(RefCell::new(0));
'loop_: while ((*i.borrow()) < N) {
let __rhs = (*i.borrow());
({ (*map1.borrow_mut()).subscript(i.as_pointer()) }).write(__rhs);
({ (*map1.borrow_mut()).subscript(i.as_pointer(), || 0_i32) }).write(__rhs);
(*i.borrow_mut()).prefix_inc();
}
}
Expand All @@ -234,10 +234,11 @@ fn main_0() -> i32 {
'loop_: while ((*i.borrow()) < N) {
assert!(
({
((({ (*map2.borrow_mut()).subscript(i.as_pointer()) }).read()) == (*i.borrow()))
((({ (*map2.borrow_mut()).subscript(i.as_pointer(), || 0_i32) }).read())
== (*i.borrow()))
})
);
({ (*map2.borrow_mut()).subscript(i.as_pointer()) }).with_mut(|__v| {
({ (*map2.borrow_mut()).subscript(i.as_pointer(), || 0_i32) }).with_mut(|__v| {
__v.prefix_inc();
});
(*i.borrow_mut()).prefix_inc();
Expand All @@ -248,12 +249,13 @@ fn main_0() -> i32 {
'loop_: while ((*i.borrow()) < N) {
assert!(
({
((({ (*map1.borrow_mut()).subscript(i.as_pointer()) }).read()) == (*i.borrow()))
((({ (*map1.borrow_mut()).subscript(i.as_pointer(), || 0_i32) }).read())
== (*i.borrow()))
})
);
assert!(
({
((({ (*map2.borrow_mut()).subscript(i.as_pointer()) }).read())
((({ (*map2.borrow_mut()).subscript(i.as_pointer(), || 0_i32) }).read())
== ((*i.borrow()) + 1))
})
);
Expand Down
Loading
Loading