Skip to content

Commit a5c0693

Browse files
Map stream-buffer iterators to shared stream references
Signed-off-by: Perfloop Agent <agent@perfloop.ai> Signed-off-by: Tomás Senart <ts@perfloop.ai>
1 parent 161d3bf commit a5c0693

6 files changed

Lines changed: 257 additions & 0 deletions

File tree

‎rules/fstream/src.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ using t1 = std::ifstream;
99
using t2 = std::ofstream;
1010
template <typename T1> using t3 = std::ostream_iterator<T1>;
1111
using t4 = std::ostream_iterator<char>::ostream_type;
12+
template <typename T1> using t5 = std::istreambuf_iterator<T1>;
1213

1314
std::ofstream f1(const char *filename, std::ios_base::openmode mode) {
1415
return std::ofstream(filename, mode);

‎rules/fstream/tgt_refcount.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ fn t3<T1>() -> Ptr<libcc2rs::CppStream> {
2121
Ptr::null()
2222
}
2323

24+
fn t5<T1>() -> Ptr<libcc2rs::CppStream> {
25+
Ptr::null()
26+
}
27+
2428
fn f2<T1>(a0: Ptr<libcc2rs::CppStream>) -> Ptr<libcc2rs::CppStream> {
2529
a0
2630
}

‎rules/fstream/tgt_unsafe.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ unsafe fn f1(a0: *const libc::c_char) -> libcc2rs::CppStream {
2929
.unwrap()
3030
}
3131

32+
fn t5<T1>() -> *mut libcc2rs::CppStream {
33+
std::ptr::null_mut()
34+
}
35+
3236
unsafe fn f2(a0: *mut libcc2rs::CppStream) -> *mut libcc2rs::CppStream {
3337
a0
3438
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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+
thread_local!(
10+
pub static data_0: Value<Box<[u8]>> = Rc::new(RefCell::new(Box::new([0_u8, 127_u8, 255_u8])));
11+
);
12+
pub fn check_1(s: Ptr<Vec<i8>>) {
13+
assert!(({ ({ ((*s.upgrade().deref()).len() - 1) } == { ::std::mem::size_of::<[u8; 3]>() }) }));
14+
let mut i: u32 = 0_u32;
15+
'loop_: while ((i as usize) < ::std::mem::size_of::<[u8; 3]>()) {
16+
assert!(
17+
({ (((index!(s, (i as usize)).read()) as u8) as i32) } == {
18+
(({
19+
let __idx0 = (i) as usize;
20+
data_0.with(|rc| rc.borrow()[__idx0])
21+
}) as i32)
22+
})
23+
);
24+
i.prefix_inc();
25+
}
26+
}
27+
pub fn main() {
28+
__cpp2rust_init_globals();
29+
std::process::exit(main_0());
30+
}
31+
fn main_0() -> i32 {
32+
let file: Value<Box<[i8]>> = Rc::new(RefCell::new(i8::array_from_literal(
33+
b"test_stdcopy_istream.txt\0",
34+
)));
35+
let mut fd: i32 = {
36+
let __mode = match Box::<[VaArg]>::from([(384).into()]).first() {
37+
Some(__m) => nix::sys::stat::Mode::from_bits_truncate(i32::get(__m) as ::libc::mode_t),
38+
None => nix::sys::stat::Mode::empty(),
39+
};
40+
match nix::fcntl::open(
41+
(file.as_pointer() as Ptr<i8>).to_rust_string().as_str(),
42+
nix::fcntl::OFlag::from_bits_retain(
43+
((::libc::O_WRONLY | ::libc::O_CREAT) | ::libc::O_TRUNC),
44+
),
45+
__mode,
46+
) {
47+
Ok(__ofd) => FdRegistry::register(__ofd),
48+
Err(__e) => {
49+
libcc2rs::cpp2rust_errno().write(__e as i32);
50+
-1
51+
}
52+
}
53+
};
54+
assert!((fd >= 0));
55+
assert!(
56+
((match FdRegistry::with_fd(fd, |__fd| {
57+
((data_0.with(|v| v.as_pointer()) as Ptr<u8>) as Ptr<u8>)
58+
.to_any()
59+
.reinterpret_cast::<u8>()
60+
.with_slice(::std::mem::size_of::<[u8; 3]>(), |__buf| {
61+
nix::unistd::write(__fd, __buf)
62+
})
63+
}) {
64+
Ok(__n) => __n as isize,
65+
Err(__e) => {
66+
libcc2rs::cpp2rust_errno().write(__e as i32);
67+
-1
68+
}
69+
} as usize)
70+
== ::std::mem::size_of::<[u8; 3]>())
71+
);
72+
assert!((FdRegistry::close(fd) == 0));
73+
{
74+
let f: Value<libcc2rs::CppStream> = Rc::new(RefCell::new(
75+
::std::fs::File::open((file.as_pointer() as Ptr<i8>).to_string())
76+
.map(libcc2rs::CppStream::from)
77+
.expect("Failed to open file"),
78+
));
79+
let s: Value<Vec<i8>> = Rc::new(RefCell::new({
80+
use std::io::Read;
81+
let mut __bytes: Vec<u8> = Vec::new();
82+
(f.as_pointer())
83+
.with_mut(|__f| __f.read_to_end(&mut __bytes))
84+
.expect("couldn't read the file");
85+
__bytes.push(0);
86+
CChar::from_byte_vec(__bytes)
87+
}));
88+
({ check_1(s.as_pointer()) });
89+
}
90+
{
91+
let f: Value<libcc2rs::CppStream> = Rc::new(RefCell::new(
92+
::std::fs::File::open((file.as_pointer() as Ptr<i8>).to_string())
93+
.map(libcc2rs::CppStream::from)
94+
.expect("Failed to open file"),
95+
));
96+
let mut begin: Ptr<libcc2rs::CppStream> = f.as_pointer();
97+
let mut copy: Ptr<libcc2rs::CppStream> = (begin).clone();
98+
let mut end: Ptr<libcc2rs::CppStream> = Ptr::null();
99+
let s: Value<Vec<i8>> = Rc::new(RefCell::new({
100+
use std::io::Read;
101+
let mut __bytes: Vec<u8> = Vec::new();
102+
(copy)
103+
.clone()
104+
.with_mut(|__f| __f.read_to_end(&mut __bytes))
105+
.expect("couldn't read the file");
106+
__bytes.push(0);
107+
CChar::from_byte_vec(__bytes)
108+
}));
109+
({ check_1(s.as_pointer()) });
110+
}
111+
match nix::unistd::unlink((file.as_pointer() as Ptr<i8>).to_rust_string().as_str()) {
112+
Ok(()) => 0,
113+
Err(__e) => {
114+
libcc2rs::cpp2rust_errno().write(__e as i32);
115+
-1
116+
}
117+
};
118+
return 0;
119+
}
120+
pub fn __cpp2rust_init_globals() {
121+
let _ = data_0.with(|_| ());
122+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
pub static mut data_0: std::cell::LazyCell<[u8; 3]> =
10+
std::cell::LazyCell::new(|| unsafe { [0_u8, 127_u8, 255_u8] });
11+
pub unsafe fn check_1(s: *const Vec<libc::c_char>) {
12+
assert!((((*s).len() - 1) == (::std::mem::size_of::<[u8; 3]>())));
13+
let mut i: u32 = 0_u32;
14+
'loop_: while ((i as usize) < (::std::mem::size_of::<[u8; 3]>())) {
15+
assert!(
16+
((((&(*s))[(i as usize)] as u8) as i32)
17+
== ((*std::cell::LazyCell::force_mut(&mut *&raw mut data_0))[(i) as usize] as i32))
18+
);
19+
i.prefix_inc();
20+
}
21+
}
22+
pub fn main() {
23+
unsafe {
24+
__cpp2rust_init_globals();
25+
std::process::exit(main_0() as i32);
26+
}
27+
}
28+
unsafe fn main_0() -> i32 {
29+
let file: [libc::c_char; 25] = std::mem::transmute(*b"test_stdcopy_istream.txt\0");
30+
let mut fd: i32 = (unsafe {
31+
libc::open(
32+
file.as_ptr() as *const i8,
33+
(((::libc::O_WRONLY) | (::libc::O_CREAT)) | (::libc::O_TRUNC)) as i32,
34+
(384),
35+
)
36+
});
37+
assert!(((fd) >= (0)));
38+
assert!(
39+
((libc::write(
40+
fd,
41+
((*std::cell::LazyCell::force_mut(&mut *&raw mut data_0)).as_ptr()
42+
as *const ::libc::c_void),
43+
::std::mem::size_of::<[u8; 3]>()
44+
) as usize)
45+
== (::std::mem::size_of::<[u8; 3]>()))
46+
);
47+
assert!(((libc::close(fd)) == (0)));
48+
{
49+
let mut f: libcc2rs::CppStream =
50+
::std::fs::File::open(::std::ffi::CStr::from_ptr(file.as_ptr()).to_str().unwrap())
51+
.map(libcc2rs::CppStream::from)
52+
.unwrap();
53+
let mut s: Vec<libc::c_char> = {
54+
let mut __bytes: Vec<u8> = Vec::new();
55+
(*(&mut f as *mut libcc2rs::CppStream))
56+
.read_to_end(&mut __bytes)
57+
.expect("couldn't read the file");
58+
let mut __buf: Vec<libc::c_char> = __bytes.iter().map(|&b| b as libc::c_char).collect();
59+
__buf.push(0);
60+
__buf
61+
};
62+
(unsafe { check_1(&s) });
63+
}
64+
{
65+
let mut f: libcc2rs::CppStream =
66+
::std::fs::File::open(::std::ffi::CStr::from_ptr(file.as_ptr()).to_str().unwrap())
67+
.map(libcc2rs::CppStream::from)
68+
.unwrap();
69+
let mut begin: *mut libcc2rs::CppStream = (&mut f as *mut libcc2rs::CppStream);
70+
let mut copy: *mut libcc2rs::CppStream = (begin).clone();
71+
let mut end: *mut libcc2rs::CppStream = std::ptr::null_mut();
72+
let mut s: Vec<libc::c_char> = {
73+
let mut __bytes: Vec<u8> = Vec::new();
74+
(*(copy).clone())
75+
.read_to_end(&mut __bytes)
76+
.expect("couldn't read the file");
77+
let mut __buf: Vec<libc::c_char> = __bytes.iter().map(|&b| b as libc::c_char).collect();
78+
__buf.push(0);
79+
__buf
80+
};
81+
(unsafe { check_1(&s) });
82+
}
83+
libc::unlink(file.as_ptr());
84+
return 0;
85+
}
86+
pub unsafe fn __cpp2rust_init_globals() {
87+
std::cell::LazyCell::force(&*&raw const data_0);
88+
}

‎tests/unit/stdcopy_istream.cpp‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <cassert>
2+
#include <fcntl.h>
3+
#include <fstream>
4+
#include <iterator>
5+
#include <string>
6+
#include <unistd.h>
7+
8+
const unsigned char data[] = {0, 127, 255};
9+
10+
void check(const std::string &s) {
11+
assert(s.size() == sizeof(data));
12+
for (unsigned i = 0; i < sizeof(data); ++i)
13+
assert(static_cast<unsigned char>(s[i]) == data[i]);
14+
}
15+
16+
int main() {
17+
const char file[] = "test_stdcopy_istream.txt";
18+
int fd = open(file, O_WRONLY | O_CREAT | O_TRUNC, 0600);
19+
assert(fd >= 0);
20+
assert(write(fd, data, sizeof(data)) == sizeof(data));
21+
assert(close(fd) == 0);
22+
{
23+
std::ifstream f(file, std::ios::binary);
24+
std::string s((std::istreambuf_iterator<char>(f.rdbuf())),
25+
std::istreambuf_iterator<char>());
26+
check(s);
27+
}
28+
{
29+
std::ifstream f(file, std::ios::binary);
30+
std::istreambuf_iterator<char> begin(f.rdbuf());
31+
std::istreambuf_iterator<char> copy(begin);
32+
std::istreambuf_iterator<char> end;
33+
std::string s(copy, end);
34+
check(s);
35+
}
36+
unlink(file);
37+
return 0;
38+
}

0 commit comments

Comments
 (0)