1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
//! An analog of a Python tuple, will accept an undefined number of any other supported types.
//!
//! You can construct it using the [pytuple!](../../macro.pytuple!.html) macro, ie:
//!
//! ```
//! # #[macro_use] extern crate rustypy;
//! # fn main(){
//! use rustypy::PyArg;
//! pytuple!(PyArg::I64(10), PyArg::F32(10.5));
//! # }
//! ```
//!
//! You must pass the variety of the argument using the PyArg enum.
//!
//! When extracting elements in Python with the FFI, elements are copied, not moved unless
//! possible (ie. content of inner containers may or may not be moved out),
//! and when free'd all the original elements are dropped.
//!
//! PyTuples behave exactly as Python tuples: they are immutable, but provide interior mutability.
//! For example, you can pop elements from an inner PyList, although the PyList cannot be moved
//! out of a PyTuple (without completely destructuring it).
//!
//! # Safety
//! PyTuple must be passed between Rust and Python as a raw pointer. You can get a
//! raw pointer using ```into_raw``` and convert from a raw pointer using the "static"
//! method ```PyDict::from_ptr``` which is unsafe as it requires dereferencing a raw pointer.
//!
//! ## Unpacking PyTuple from Python
//! Is recommended to use the [unpack_pytuple!](../../macro.unpack_pytuple!.html) macro in order
//! to convert a PyTuple to a Rust native type. Check the macro documentation for more info.

use std::iter::IntoIterator;
use std::mem;
use std::ops::Deref;

use crate::pytypes::PyArg;

/// An analog of a Python tuple, will accept an undefined number of other
/// [supported types](../../../rustypy/pytypes/enum.PyArg.html).
///
/// Read the [module docs](index.html) for more information.
#[derive(Clone, Debug, PartialEq)]
pub struct PyTuple {
    pub(crate) elem: PyArg,
    pub(crate) idx: usize,
    pub(crate) next: Option<Box<PyTuple>>,
}

#[allow(clippy::len_without_is_empty)]
impl<'a> PyTuple {
    #[doc(hidden)]
    pub fn new(elem: PyArg, idx: usize, next: Option<Box<PyTuple>>) -> PyTuple {
        PyTuple { elem, idx, next }
    }

    #[doc(hidden)]
    pub fn set_next(&mut self, next: Option<Box<PyTuple>>) {
        self.next = next;
    }

    /// Get a PyTuple from a previously boxed raw pointer.
    pub unsafe fn from_ptr(ptr: *mut PyTuple) -> PyTuple {
        *(Box::from_raw(ptr))
    }

    /// Get a mutable reference to an inner element of the tuple, takes as argument the position
    /// of the element and returns a Result.
    pub(crate) fn as_mut(&mut self, idx: usize) -> Result<&mut PyArg, &str> {
        if idx == self.idx {
            Ok(&mut self.elem)
        } else {
            match self.next {
                Some(ref mut e) => (**e).as_mut(idx),
                None => Err("PyTuple index out of range."),
            }
        }
    }

    #[doc(hidden)]
    pub fn replace_elem(&mut self, idx: usize) -> Result<PyArg, &str> {
        if idx == self.idx {
            let e = mem::replace(&mut self.elem, PyArg::None);
            Ok(e)
        } else {
            match self.next {
                Some(ref mut e) => (**e).replace_elem(idx),
                None => Err("PyTuple index out of range."),
            }
        }
    }

    /// Get a regular reference to an inner element of the tuple, takes as argument the position
    /// of the element and returns a Result.
    pub(crate) fn as_ref(&self, idx: usize) -> Result<&PyArg, &str> {
        if idx == self.idx {
            Ok(&self.elem)
        } else {
            match self.next {
                Some(ref e) => (**e).as_ref(idx),
                None => Err("PyTuple index out of range."),
            }
        }
    }

    fn push(&mut self, next: PyTuple) {
        self.next = Some(Box::new(next));
    }

    /// Returns the length of the tuple.
    pub fn len(&self) -> usize {
        match self.next {
            Some(ref e) => e.len(),
            None => self.idx + 1,
        }
    }

    /// Returns self as raw pointer. Use this method when returning a PyTuple to Python.
    pub fn into_raw(self) -> *mut PyTuple {
        Box::into_raw(Box::new(self))
    }
}

impl<'a> IntoIterator for &'a PyTuple {
    type Item = &'a PyArg;
    type IntoIter = ::std::vec::IntoIter<&'a PyArg>;
    fn into_iter(self) -> Self::IntoIter {
        let l = self.len();
        let mut iter = Vec::with_capacity(l);
        for i in 0..l {
            iter.push(self.as_ref(i).unwrap());
        }
        iter.into_iter()
    }
}

impl Deref for PyTuple {
    type Target = PyArg;

    fn deref(&self) -> &PyArg {
        &self.elem
    }
}

/// This macro allows the construction of [PyTuple](../rustypy/pytypes/pytuple/struct.PyTuple.html)
/// types.
///
/// # Examples
///
/// ```
/// # #[macro_use] extern crate rustypy;
/// # fn main(){
/// # use rustypy::PyArg;
/// pytuple!(PyArg::I64(10), PyArg::F32(10.5));
/// # }
/// ```
///
#[macro_export]
macro_rules! pytuple {
    ( $( $elem:ident ),+ ) => {{
        use rustypy::PyTuple;
        let mut cnt;
        let mut tuple = Vec::new();
        cnt = 0usize;
        $(
            let tuple_e = PyTuple::new(
                $elem,
                cnt,
                None,
            );
            tuple.push(tuple_e);
            cnt += 1;
        )*;
        if cnt == tuple.len() {}; // stub to remove warning...
        let t_len = tuple.len() - 1;
        for i in 1..(t_len + 1) {
            let idx = t_len - i;
            let last = tuple.pop().unwrap();
            let prev = tuple.get_mut(idx).unwrap();
            prev.set_next(Some(Box::new(last)));
        }
        tuple.pop().unwrap()
    }};
    ( $( $elem:expr ),+ ) => {{
        use rustypy::PyTuple;
        let mut cnt;
        let mut tuple = Vec::new();
        cnt = 0usize;
        $(
            let tuple_e = PyTuple::new(
                $elem,
                cnt,
                None,
            );
            tuple.push(tuple_e);
            cnt += 1;
        )*;
        let t_len = cnt - 1;
        for i in 1..cnt {
            let idx = t_len - i;
            let last = tuple.pop().unwrap();
            let prev = tuple.get_mut(idx).unwrap();
            prev.set_next(Some(Box::new(last)));
        }
        tuple.pop().unwrap()
    }};
}

#[doc(hidden)]
#[no_mangle]
pub unsafe extern "C" fn pytuple_new(idx: usize, elem: *mut PyArg) -> *mut PyTuple {
    let tuple = PyTuple {
        elem: *(Box::from_raw(elem)),
        idx,
        next: None,
    };
    tuple.into_raw()
}

#[doc(hidden)]
#[no_mangle]
pub unsafe extern "C" fn pytuple_push(next: *mut PyTuple, prev: &mut PyTuple) {
    let next: PyTuple = *(Box::from_raw(next));
    prev.push(next)
}

#[doc(hidden)]
#[no_mangle]
pub unsafe extern "C" fn pytuple_free(ptr: *mut PyTuple) {
    if ptr.is_null() {
        return;
    }

    Box::from_raw(ptr);
}

#[doc(hidden)]
#[no_mangle]
pub unsafe extern "C" fn pytuple_len(ptr: *mut PyTuple) -> usize {
    let tuple = &*ptr;
    tuple.len()
}

#[doc(hidden)]
#[no_mangle]
pub unsafe extern "C" fn pytuple_get_element(ptr: *mut PyTuple, index: usize) -> *mut PyArg {
    let tuple = &mut *ptr;
    let elem = &PyTuple::as_mut(tuple, index).unwrap();
    let copied: PyArg = (*elem).clone();
    Box::into_raw(Box::new(copied))
}