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
8 changes: 0 additions & 8 deletions docs/src/runtime/iterators.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,6 @@ for x in v.as_pointer() as Ptr<i32> {
}
```

`PtrValueIter` yields copies of the elements instead of pointers to them; rule
bodies use it to feed a range of C memory to Rust iterator adaptors:

```rust
// std::accumulate(first, last, init)
let count = (last - first) as usize;
PtrValueIter::new(&first, count).fold(init, |acc, x| acc + x)
```

## Stable iterators

Expand Down
37 changes: 0 additions & 37 deletions libcc2rs/src/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1229,43 +1229,6 @@ impl<T> Iterator for Ptr<T> {
}
}

// Ptr iterator that yields values instead of pointers.
// It's more efficient and it's useful to implement idiomatic iterator patterns
pub struct PtrValueIter<T> {
ptr: Ptr<T>,
n: usize,
}

impl<T> PtrValueIter<T> {
pub fn new(ptr: &Ptr<T>, n: usize) -> Self {
Self {
ptr: ptr.clone(),
n,
}
}
}

impl<T: Clone + ByteRepr> Iterator for PtrValueIter<T> {
type Item = T;

fn next(&mut self) -> Option<Self::Item> {
if self.n > 0 {
let value = self.ptr.read();
self.ptr += 1;
self.n -= 1;
Some(value)
} else {
None
}
}

fn size_hint(&self) -> (usize, Option<usize>) {
(self.n, Some(self.n))
}
}

impl<T: Clone + ByteRepr> ExactSizeIterator for PtrValueIter<T> {}

impl<T> Sub for Ptr<T> {
type Output = isize;
fn sub(self, other: Self) -> Self::Output {
Expand Down
5 changes: 2 additions & 3 deletions rules/algorithm/src.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ void f12(T1 first, T1 last, const T2 &value) {
return std::fill(first, last, value);
}

std::ostream_iterator<char> f13(std::string::iterator a0,
std::string::iterator a1,
std::ostream_iterator<char> a2) {
template <typename T1>
std::ostream_iterator<char> f13(T1 a0, T1 a1, std::ostream_iterator<char> a2) {
return std::copy(a0, a1, a2);
}

Expand Down
12 changes: 8 additions & 4 deletions rules/algorithm/tgt_refcount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,14 @@ fn f12<T1: CppInputIterator<T2>, T2: Clone + ByteRepr>(a0: T1, a1: T1, a2: T2) {
}
}

fn f13(a0: Ptr<i8>, a1: Ptr<i8>, a2: &mut ::std::fs::File) -> ::std::fs::File {
a0.with_slice(a1.get_offset() - a0.get_offset(), |__s| {
CChar::with_u8_slice(__s, |__b| a2.write_all(__b))
});
fn f13<T1: CppInputIterator<i8>>(a0: T1, a1: T1, a2: &mut ::std::fs::File) -> ::std::fs::File {
let mut __bytes: Vec<u8> = Vec::new();
let mut __it = a0;
while __it != a1 {
__bytes.push(__it.iter_deref().read() as u8);
__it.iter_inc();
}
a2.write_all(&__bytes);
a2.try_clone().unwrap()
}

Expand Down
17 changes: 10 additions & 7 deletions rules/algorithm/tgt_unsafe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,18 @@ unsafe fn f12<T1: UnsafeCppInputIterator<T2>, T2: Clone>(a0: T1, a1: T1, a2: T2)
}
}

unsafe fn f13(
a0: *const libc::c_char,
a1: *const libc::c_char,
unsafe fn f13<T1: UnsafeCppInputIterator<libc::c_char>>(
a0: T1,
a1: T1,
a2: &mut ::std::fs::File,
) -> ::std::fs::File {
let __start = a0 as *const u8;
let __end = a1 as *const u8;
let __len = __end.offset_from(__start) as usize;
a2.write_all(::std::slice::from_raw_parts(__start, __len));
let mut __bytes: Vec<u8> = Vec::new();
let mut __it = a0;
while __it != a1 {
__bytes.push(*__it.iter_deref() as u8);
__it.iter_inc();
}
a2.write_all(&__bytes);
a2.try_clone().unwrap()
}

Expand Down
20 changes: 11 additions & 9 deletions rules/iterator_category/shim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,14 +338,6 @@ macro_rules! random_access_iterator {
Vec($ptr),
}

impl<T> $name<T> {
pub fn vec_ptr(&self) -> $ptr {
match self {
Self::Vec(p) => p.clone(),
}
}
}

impl<T> Default for $name<T> {
fn default() -> Self {
Self::Vec($null)
Expand All @@ -354,7 +346,9 @@ macro_rules! random_access_iterator {

impl<T> Clone for $name<T> {
fn clone(&self) -> Self {
Self::Vec(self.vec_ptr())
match self {
Self::Vec(p) => Self::Vec(p.clone()),
}
}
}

Expand Down Expand Up @@ -442,6 +436,14 @@ random_access_iterator!(
Ptr::null()
);

impl<T> RandomAccessIterator<T> {
pub fn index(&self) -> usize {
match self {
Self::Vec(p) => p.get_offset(),
}
}
}

random_access_iterator!(
UnsafeRandomAccessIterator,
*mut T,
Expand Down
5 changes: 0 additions & 5 deletions rules/string/src.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include <string>

using t1 = std::string;
using t2 = std::string::iterator;
typedef std::string::size_type t3;

std::string f1(const std::string &s, std::size_t pos, std::size_t count) {
Expand Down Expand Up @@ -57,10 +56,6 @@ bool f18(const std::string &a, const char *b) { return a == b; }

std::size_t f19(const std::string &s) { return s.length(); }

std::string::iterator f20(std::string::iterator it, std::size_t n) {
return it + n;
}

std::string &f21(std::string &s, std::size_t n, char c) {
return s.append(n, c);
}
Expand Down
16 changes: 4 additions & 12 deletions rules/string/tgt_refcount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ fn t1() -> Vec<i8> {
vec![0]
}

fn t2() -> Ptr<i8> {
Ptr::null()
}

fn f1(a0: Vec<i8>, a1: usize, a2: usize) -> Vec<i8> {
let mut __tmp1 =
a0[(a1) as usize..::std::cmp::min((a1 + a2) as usize, a0.len().saturating_sub(1))].to_vec();
Expand Down Expand Up @@ -62,8 +58,8 @@ fn f11(a0: Ptr<i8>) -> Ptr<i8> {
a0
}

fn f12(a0: Ptr<i8>) -> Ptr<i8> {
a0
fn f12(a0: Ptr<i8>) -> RandomAccessIterator<i8> {
RandomAccessIterator::Vec(a0)
}

fn f14(a0: Ptr<Vec<i8>>, a1: usize, a2: usize, a3: Ptr<i8>, a4: usize) -> Ptr<Vec<i8>> {
Expand All @@ -80,8 +76,8 @@ fn f14(a0: Ptr<Vec<i8>>, a1: usize, a2: usize, a3: Ptr<i8>, a4: usize) -> Ptr<Ve
a0
}

fn f15(a0: Ptr<i8>) -> Ptr<i8> {
a0.to_last()
fn f15(a0: Ptr<i8>) -> RandomAccessIterator<i8> {
RandomAccessIterator::Vec(a0.to_last())
}

fn f16(a0: Vec<i8>, a1: Ptr<i8>) -> usize {
Expand All @@ -106,10 +102,6 @@ fn f18(a0: Vec<i8>, a1: Ptr<i8>) -> bool {
a1.with_c_str(|__s| a0[..a0.len().saturating_sub(1)] == *__s)
}

fn f20(a0: Ptr<i8>, a1: usize) -> Ptr<i8> {
a0.offset(a1 as isize)
}

// TODO: This should return a0
fn f21(a0: &mut Vec<i8>, a1: usize, a2: i8) -> Vec<i8> {
a0.pop();
Expand Down
17 changes: 6 additions & 11 deletions rules/string/tgt_unsafe.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
// Copyright (c) 2022-present INESC-ID.
// Distributed under the MIT license that can be found in the LICENSE file.

use libcc2rs::*;

fn t1() -> Vec<libc::c_char> {
vec![0]
}

fn t2() -> *mut libc::c_char {
::std::ptr::null_mut()
}

fn t3() -> usize {
0_usize
}
Expand Down Expand Up @@ -80,8 +78,8 @@ unsafe fn f10(a0: *const libc::c_char) -> Vec<libc::c_char> {
unsafe fn f11(a0: Vec<libc::c_char>) -> *const libc::c_char {
a0.as_ptr()
}
unsafe fn f12(a0: &mut Vec<libc::c_char>) -> *mut libc::c_char {
a0.as_mut_ptr()
unsafe fn f12(a0: &mut Vec<libc::c_char>) -> UnsafeRandomAccessIterator<libc::c_char> {
UnsafeRandomAccessIterator::Vec(a0.as_mut_ptr())
}
unsafe fn f13(a0: &mut Vec<libc::c_char>, a1: usize) {
a0.pop();
Expand All @@ -100,8 +98,8 @@ unsafe fn f14(
::std::slice::from_raw_parts(a3, a4 as usize).to_vec(),
);
}
unsafe fn f15(a0: &mut Vec<libc::c_char>) -> *mut libc::c_char {
a0.as_mut_ptr().add(a0.len() - 1)
unsafe fn f15(a0: &mut Vec<libc::c_char>) -> UnsafeRandomAccessIterator<libc::c_char> {
UnsafeRandomAccessIterator::Vec(a0.as_mut_ptr().add(a0.len() - 1))
}
unsafe fn f16(a0: Vec<libc::c_char>, a1: *const libc::c_char) -> usize {
match a0.iter().rposition(|&c| {
Expand Down Expand Up @@ -134,9 +132,6 @@ unsafe fn f18(a0: Vec<libc::c_char>, a1: *const libc::c_char) -> bool {
unsafe fn f19(a0: Vec<libc::c_char>) -> usize {
(a0.len() - 1)
}
unsafe fn f20(a0: *mut libc::c_char, a1: usize) -> *mut libc::c_char {
a0.add(a1 as usize)
}
unsafe fn f21(a0: &mut Vec<libc::c_char>, a1: usize, a2: libc::c_char) {
a0.splice(
a0.len() - 1..a0.len() - 1,
Expand Down
25 changes: 17 additions & 8 deletions rules/vector/src.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,18 +136,13 @@ void f80(std::vector<T1, T2> &o, const T1 &value) {
return o.push_back(value);
}

template <typename T1, typename T2 = std::allocator<T1>>
std::vector<T1, T2> f90(const T1 *first, const T1 *last) {
return std::vector<T1, T2>(first, last);
}

template <typename T1, typename T2 = std::allocator<T1>>
std::vector<T1, T2> f91(const std::initializer_list<T1> &a0) {
return std::vector<T1, T2>(a0);
}

template <typename T1, typename T2 = std::allocator<T1>, typename T3>
std::vector<T1, T2> f92(T3 *first, T3 *last) {
std::vector<T1, T2> f92(T3 first, T3 last) {
return std::vector<T1, T2>(first, last);
}

Expand Down Expand Up @@ -181,10 +176,10 @@ const T1 &f99(const std::vector<T1, T2> &o) {
return o.back();
}

template <typename T1, typename T2 = std::allocator<T1>>
template <typename T1, typename T2 = std::allocator<T1>, typename T3>
typename std::vector<T1, T2>::iterator
f101(std::vector<T1, T2> &o, typename std::vector<T1, T2>::const_iterator pos,
const T1 *first, const T1 *last) {
T3 first, T3 last) {
return o.insert(pos, first, last);
}

Expand Down Expand Up @@ -231,3 +226,17 @@ auto f114(std::vector<T1, T2> &o, Init<T1, Args> &&...args)
-> decltype(o.emplace_back(std::forward<Args>(args)...)) {
return o.emplace_back(std::forward<Args>(args)...);
}

template <typename T1, typename T2 = std::allocator<T1>>
typename std::vector<T1, T2>::iterator
f115(std::vector<T1, T2> &o, typename std::vector<T1, T2>::const_iterator pos,
std::size_t n, const T1 &value) {
return o.insert(pos, n, value);
}

template <typename T1, typename T2 = std::allocator<T1>>
typename std::vector<T1, T2>::iterator
f116(std::vector<T1, T2> &o, typename std::vector<T1, T2>::const_iterator pos,
std::initializer_list<T1> l) {
return o.insert(pos, l);
}
Loading
Loading