Skip to main content

miniextendr_api/from_r/
references.rs

1//! Reference conversions (borrowed views into R vectors).
2//!
3//! Provides zero-copy access to R vector data through references.
4//! Callers must keep the source SEXP rooted for the entire reference lifetime.
5//!
6//! Covers: `&T`, `&mut T`, `Option<&T>`, `Vec<&T>`, `Vec<&[T]>`, and
7//! mutable variants for all `RNativeType` types.
8//!
9//! # Tradeoff
10//!
11//! Prefer borrowed `&[T]` over owned `Vec<T>` when you only need read access —
12//! this is the fast path (no allocation, no copy). Reach for `&mut [T]` only
13//! when you genuinely need to mutate R-owned data in place; the R caller will
14//! observe those writes (ALTREP MAYBE_REFERENCED rules still apply). Failure
15//! mode: taking `&mut [T]` from a shared SEXP that R has handed to multiple
16//! callers writes through that aliased buffer.
17//!
18//! For NA-aware reads use [`crate::from_r::na_vectors`] (`Vec<Option<T>>`) —
19//! borrowed slices cannot express NA without losing the sentinel encoding.
20//! Outbound borrowed slices copy into R-owned storage through [`crate::IntoR`].
21//! Mutable list inputs preflight all raw ranges before creating any references;
22//! this does not establish the root lifetime or exclude borrows held elsewhere
23//! by a direct caller (the remaining lifetime-boundary work is #1427).
24
25use crate::from_r::prepared::{Prepared, native_leaf};
26use crate::from_r::{
27    NativeBorrow, SexpError, SexpLengthError, SexpTypeError, TryFromSexp, map_vecsxp_batched,
28    map_vecsxp_with, r_slice_mut,
29};
30use crate::{RLogical, RNativeType, SEXP, SEXPTYPE, SexpExt};
31
32/// An unborrowed native buffer. No Rust reference exists during list preflight.
33#[derive(#[automatically_derived]impl<T: ::core::clone::Clone> ::core::clone::Clone for
    MutableRange<T> {
    #[inline]
    fn clone(&self) -> Self {
        MutableRange {
            data: ::core::clone::Clone::clone(&self.data),
            len: ::core::clone::Clone::clone(&self.len),
        }
    }
}Clone, #[automatically_derived]impl<T: ::core::marker::Copy> ::core::marker::Copy for
    MutableRange<T> {
}Copy)]
34struct MutableRange<T> {
35    data: *mut T,
36    len: usize,
37}
38
39impl<T> MutableRange<T> {
40    fn overlaps(&self, other: &Self) -> bool {
41        if self.len == 0 || other.len == 0 {
42            return false;
43        }
44        let a = self.data.addr();
45        let b = other.data.addr();
46        // Each range describes an existing R allocation. Compare integer
47        // addresses only; retain the original mutable pointer for borrowing.
48        if a <= b {
49            b - a < self.len * core::mem::size_of::<T>()
50        } else {
51            a - b < other.len * core::mem::size_of::<T>()
52        }
53    }
54}
55
56/// Validate every element and every raw buffer before creating any references.
57/// Different ALTREP objects can expose identical or partially overlapping
58/// storage, so comparing SEXP identities (or just starting pointers) is not
59/// enough here. NULL is a missing element only for the optional forms.
60fn map_mutable_list<T: RNativeType, U>(
61    sexp: SEXP,
62    scalar: bool,
63    optional: bool,
64    map: impl FnMut(Option<MutableRange<T>>) -> U,
65) -> Result<Vec<U>, SexpError> {
66    let mut ranges: Vec<MutableRange<T>> = Vec::new();
67    let raw = map_vecsxp_batched(sexp, |element| {
68        let actual = element.type_of();
69        if optional && actual == SEXPTYPE::NILSXP {
70            return Ok(None);
71        }
72        if actual != T::SEXP_TYPE {
73            return Err(SexpTypeError {
74                expected: T::SEXP_TYPE,
75                actual,
76            }
77            .into());
78        }
79        let len = element.len();
80        if scalar && len != 1 {
81            return Err(SexpLengthError {
82                expected: 1,
83                actual: len,
84            }
85            .into());
86        }
87        let range = MutableRange {
88            data: unsafe { T::dataptr_mut(element) },
89            len,
90        };
91        let overlaps = ranges.iter().any(|previous| range.overlaps(previous));
92        ranges.push(range);
93        if overlaps {
94            Err(SexpError::InvalidValue(
95                "list contains overlapping native buffers; cannot create multiple mutable references"
96                    .to_string(),
97            ))
98        } else {
99            Ok(Some(range))
100        }
101    })?;
102    // Only Rust allocations remain; a failed preflight never constructs &mut.
103    Ok(raw.into_iter().map(map).collect())
104}
105
106/// The checks of a native scalar reference conversion: `sexp` has `T`'s R
107/// type and length one. `len` reads the length, only once the type matched
108/// (a rejected ALTREP of another type must not run its Length callback).
109/// Shared with its preparation, which defers the reference (#1504).
110#[inline]
111fn native_scalar<T: RNativeType>(sexp: SEXP, len: impl FnOnce() -> usize) -> Result<(), SexpError> {
112    let actual = sexp.type_of();
113    if actual != T::SEXP_TYPE {
114        return Err(SexpTypeError {
115            expected: T::SEXP_TYPE,
116            actual,
117        }
118        .into());
119    }
120    let len = len();
121    if len != 1 {
122        return Err(SexpLengthError {
123            expected: 1,
124            actual: len,
125        }
126        .into());
127    }
128    Ok(())
129}
130
131/// The preparation of `Option<&T>` / `Option<&mut T>`: `NULL` is `None`,
132/// anything else is a scalar reference.
133fn optional_native_scalar<T: RNativeType, R: TryFromSexp<Error = SexpError>>(
134    sexp: SEXP,
135    none: impl FnOnce() -> R,
136) -> Prepared<R> {
137    if sexp.type_of() == SEXPTYPE::NILSXP {
138        Prepared::Ready(none())
139    } else {
140        native_leaf(native_scalar::<T>(sexp, || sexp.len()))
141    }
142}
143
144macro_rules! impl_ref_conversions_for {
145    ($t:ty) => {
146        impl<'a> TryFromSexp for &'a $t {
147            const NATIVE_BORROW: Option<NativeBorrow> =
148                Some(NativeBorrow::scalar(<$t as RNativeType>::SEXP_TYPE, false));
149
150            type Error = SexpError;
151
152            #[inline]
153            fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
154                native_scalar::<$t>(sexp, || sexp.len())?;
155                unsafe { sexp.as_slice::<$t>() }.first().ok_or_else(|| {
156                    SexpLengthError {
157                        expected: 1,
158                        actual: 0,
159                    }
160                    .into()
161                })
162            }
163
164            #[inline]
165            unsafe fn try_from_sexp_unchecked(sexp: SEXP) -> Result<Self, Self::Error> {
166                native_scalar::<$t>(sexp, || unsafe { sexp.len_unchecked() })?;
167                unsafe { sexp.as_slice_unchecked::<$t>() }
168                    .first()
169                    .ok_or_else(|| {
170                        SexpLengthError {
171                            expected: 1,
172                            actual: 0,
173                        }
174                        .into()
175                    })
176            }
177
178            fn __mx_prepare(sexp: SEXP) -> Prepared<Self> {
179                native_leaf(native_scalar::<$t>(sexp, || sexp.len()))
180            }
181        }
182
183        /// # Safety note (aliasing)
184        ///
185        /// This impl can produce aliased `&mut` references if the same R object
186        /// is passed to multiple mutable parameters. The caller (generated wrapper)
187        /// rejects overlapping native mutable/shared borrows before conversion.
188        /// Direct conversion callers must enforce this borrowing contract themselves.
189        impl<'a> TryFromSexp for &'a mut $t {
190            const NATIVE_BORROW: Option<NativeBorrow> =
191                Some(NativeBorrow::scalar(<$t as RNativeType>::SEXP_TYPE, true));
192
193            type Error = SexpError;
194
195            #[inline]
196            fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
197                native_scalar::<$t>(sexp, || sexp.len())?;
198                let ptr = unsafe { <$t as RNativeType>::dataptr_mut(sexp) };
199                Ok(unsafe { &mut *ptr })
200            }
201
202            #[inline]
203            unsafe fn try_from_sexp_unchecked(sexp: SEXP) -> Result<Self, Self::Error> {
204                native_scalar::<$t>(sexp, || unsafe { sexp.len_unchecked() })?;
205                let ptr = unsafe { <$t as RNativeType>::dataptr_mut(sexp) };
206                Ok(unsafe { &mut *ptr })
207            }
208
209            fn __mx_prepare(sexp: SEXP) -> Prepared<Self> {
210                native_leaf(native_scalar::<$t>(sexp, || sexp.len()))
211            }
212        }
213
214        impl<'a> TryFromSexp for Option<&'a $t> {
215            const NATIVE_BORROW: Option<NativeBorrow> = <&'a $t as TryFromSexp>::NATIVE_BORROW;
216
217            type Error = SexpError;
218
219            #[inline]
220            fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
221                if sexp.type_of() == SEXPTYPE::NILSXP {
222                    return Ok(None);
223                }
224                let value: &'a $t = TryFromSexp::try_from_sexp(sexp)?;
225                Ok(Some(value))
226            }
227
228            #[inline]
229            unsafe fn try_from_sexp_unchecked(sexp: SEXP) -> Result<Self, Self::Error> {
230                if sexp.type_of() == SEXPTYPE::NILSXP {
231                    return Ok(None);
232                }
233                let value: &'a $t = unsafe { TryFromSexp::try_from_sexp_unchecked(sexp)? };
234                Ok(Some(value))
235            }
236
237            fn __mx_prepare(sexp: SEXP) -> Prepared<Self> {
238                optional_native_scalar::<$t, Self>(sexp, || None)
239            }
240        }
241
242        impl<'a> TryFromSexp for Option<&'a mut $t> {
243            const NATIVE_BORROW: Option<NativeBorrow> = <&'a mut $t as TryFromSexp>::NATIVE_BORROW;
244
245            type Error = SexpError;
246
247            #[inline]
248            fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
249                if sexp.type_of() == SEXPTYPE::NILSXP {
250                    return Ok(None);
251                }
252                let value: &'a mut $t = TryFromSexp::try_from_sexp(sexp)?;
253                Ok(Some(value))
254            }
255
256            #[inline]
257            unsafe fn try_from_sexp_unchecked(sexp: SEXP) -> Result<Self, Self::Error> {
258                if sexp.type_of() == SEXPTYPE::NILSXP {
259                    return Ok(None);
260                }
261                let value: &'a mut $t = unsafe { TryFromSexp::try_from_sexp_unchecked(sexp)? };
262                Ok(Some(value))
263            }
264
265            fn __mx_prepare(sexp: SEXP) -> Prepared<Self> {
266                optional_native_scalar::<$t, Self>(sexp, || None)
267            }
268        }
269
270        // Option<&[T]> and Option<&mut [T]> impls removed - now use blanket impls
271
272        impl<'a> TryFromSexp for Vec<&'a $t> {
273            const NATIVE_BORROW: Option<NativeBorrow> =
274                NativeBorrow::in_list(<&'a $t as TryFromSexp>::NATIVE_BORROW);
275
276            type Error = SexpError;
277
278            fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
279                map_vecsxp_with(sexp, |_i, elem| TryFromSexp::try_from_sexp(elem))
280            }
281        }
282
283        impl<'a> TryFromSexp for Vec<Option<&'a $t>> {
284            const NATIVE_BORROW: Option<NativeBorrow> =
285                NativeBorrow::in_list(<Option<&'a $t> as TryFromSexp>::NATIVE_BORROW);
286
287            type Error = SexpError;
288
289            fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
290                map_vecsxp_with(sexp, |_i, elem| {
291                    if elem.type_of() == SEXPTYPE::NILSXP {
292                        Ok(None)
293                    } else {
294                        let value: &'a $t = TryFromSexp::try_from_sexp(elem)?;
295                        Ok(Some(value))
296                    }
297                })
298            }
299        }
300
301        impl<'a> TryFromSexp for Vec<&'a mut $t> {
302            const NATIVE_BORROW: Option<NativeBorrow> =
303                NativeBorrow::in_list(<&'a mut $t as TryFromSexp>::NATIVE_BORROW);
304
305            type Error = SexpError;
306
307            fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
308                map_mutable_list::<$t, _>(sexp, true, false, |range| {
309                    // Non-optional scalar ranges were checked above.
310                    unsafe { &mut *range.unwrap().data }
311                })
312            }
313        }
314
315        impl<'a> TryFromSexp for Vec<Option<&'a mut $t>> {
316            const NATIVE_BORROW: Option<NativeBorrow> =
317                NativeBorrow::in_list(<Option<&'a mut $t> as TryFromSexp>::NATIVE_BORROW);
318
319            type Error = SexpError;
320
321            fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
322                map_mutable_list::<$t, _>(sexp, true, true, |range| {
323                    range.map(|range| unsafe { &mut *range.data })
324                })
325            }
326        }
327
328        impl<'a> TryFromSexp for Vec<&'a [$t]> {
329            const NATIVE_BORROW: Option<NativeBorrow> =
330                NativeBorrow::in_list(<&'a [$t] as TryFromSexp>::NATIVE_BORROW);
331
332            type Error = SexpError;
333
334            fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
335                map_vecsxp_with(sexp, |_i, elem| {
336                    TryFromSexp::try_from_sexp(elem).map_err(SexpError::from)
337                })
338            }
339        }
340
341        impl<'a> TryFromSexp for Vec<Option<&'a [$t]>> {
342            const NATIVE_BORROW: Option<NativeBorrow> =
343                NativeBorrow::in_list(<Option<&'a [$t]> as TryFromSexp>::NATIVE_BORROW);
344
345            type Error = SexpError;
346
347            fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
348                map_vecsxp_with(sexp, |_i, elem| {
349                    if elem.type_of() == SEXPTYPE::NILSXP {
350                        Ok(None)
351                    } else {
352                        let slice: &'a [$t] =
353                            TryFromSexp::try_from_sexp(elem).map_err(SexpError::from)?;
354                        Ok(Some(slice))
355                    }
356                })
357            }
358        }
359
360        impl<'a> TryFromSexp for Vec<&'a mut [$t]> {
361            const NATIVE_BORROW: Option<NativeBorrow> =
362                NativeBorrow::in_list(<&'a mut [$t] as TryFromSexp>::NATIVE_BORROW);
363
364            type Error = SexpError;
365
366            fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
367                map_mutable_list::<$t, _>(sexp, false, false, |range| {
368                    let range = range.unwrap();
369                    unsafe { r_slice_mut(range.data, range.len) }
370                })
371            }
372        }
373
374        impl<'a> TryFromSexp for Vec<Option<&'a mut [$t]>> {
375            const NATIVE_BORROW: Option<NativeBorrow> =
376                NativeBorrow::in_list(<Option<&'a mut [$t]> as TryFromSexp>::NATIVE_BORROW);
377
378            type Error = SexpError;
379
380            fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
381                map_mutable_list::<$t, _>(sexp, false, true, |range| {
382                    range.map(|range| unsafe { r_slice_mut(range.data, range.len) })
383                })
384            }
385        }
386    };
387}
388
389impl<'a> TryFromSexp for &'a i32 {
    const NATIVE_BORROW: Option<NativeBorrow> =
        Some(NativeBorrow::scalar(<i32 as RNativeType>::SEXP_TYPE, false));
    type Error = SexpError;
    #[inline]
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        native_scalar::<i32>(sexp, || sexp.len())?;
        unsafe {
                    sexp.as_slice::<i32>()
                }.first().ok_or_else(||
                { SexpLengthError { expected: 1, actual: 0 }.into() })
    }
    #[inline]
    unsafe fn try_from_sexp_unchecked(sexp: SEXP)
        -> Result<Self, Self::Error> {
        native_scalar::<i32>(sexp, || unsafe { sexp.len_unchecked() })?;
        unsafe {
                    sexp.as_slice_unchecked::<i32>()
                }.first().ok_or_else(||
                { SexpLengthError { expected: 1, actual: 0 }.into() })
    }
    fn __mx_prepare(sexp: SEXP) -> Prepared<Self> {
        native_leaf(native_scalar::<i32>(sexp, || sexp.len()))
    }
}
/// # Safety note (aliasing)
///
/// This impl can produce aliased `&mut` references if the same R object
/// is passed to multiple mutable parameters. The caller (generated wrapper)
/// rejects overlapping native mutable/shared borrows before conversion.
/// Direct conversion callers must enforce this borrowing contract themselves.
impl<'a> TryFromSexp for &'a mut i32 {
    const NATIVE_BORROW: Option<NativeBorrow> =
        Some(NativeBorrow::scalar(<i32 as RNativeType>::SEXP_TYPE, true));
    type Error = SexpError;
    #[inline]
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        native_scalar::<i32>(sexp, || sexp.len())?;
        let ptr = unsafe { <i32 as RNativeType>::dataptr_mut(sexp) };
        Ok(unsafe { &mut *ptr })
    }
    #[inline]
    unsafe fn try_from_sexp_unchecked(sexp: SEXP)
        -> Result<Self, Self::Error> {
        native_scalar::<i32>(sexp, || unsafe { sexp.len_unchecked() })?;
        let ptr = unsafe { <i32 as RNativeType>::dataptr_mut(sexp) };
        Ok(unsafe { &mut *ptr })
    }
    fn __mx_prepare(sexp: SEXP) -> Prepared<Self> {
        native_leaf(native_scalar::<i32>(sexp, || sexp.len()))
    }
}
impl<'a> TryFromSexp for Option<&'a i32> {
    const NATIVE_BORROW: Option<NativeBorrow> =
        <&'a i32 as TryFromSexp>::NATIVE_BORROW;
    type Error = SexpError;
    #[inline]
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        if sexp.type_of() == SEXPTYPE::NILSXP { return Ok(None); }
        let value: &'a i32 = TryFromSexp::try_from_sexp(sexp)?;
        Ok(Some(value))
    }
    #[inline]
    unsafe fn try_from_sexp_unchecked(sexp: SEXP)
        -> Result<Self, Self::Error> {
        if sexp.type_of() == SEXPTYPE::NILSXP { return Ok(None); }
        let value: &'a i32 =
            unsafe { TryFromSexp::try_from_sexp_unchecked(sexp)? };
        Ok(Some(value))
    }
    fn __mx_prepare(sexp: SEXP) -> Prepared<Self> {
        optional_native_scalar::<i32, Self>(sexp, || None)
    }
}
impl<'a> TryFromSexp for Option<&'a mut i32> {
    const NATIVE_BORROW: Option<NativeBorrow> =
        <&'a mut i32 as TryFromSexp>::NATIVE_BORROW;
    type Error = SexpError;
    #[inline]
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        if sexp.type_of() == SEXPTYPE::NILSXP { return Ok(None); }
        let value: &'a mut i32 = TryFromSexp::try_from_sexp(sexp)?;
        Ok(Some(value))
    }
    #[inline]
    unsafe fn try_from_sexp_unchecked(sexp: SEXP)
        -> Result<Self, Self::Error> {
        if sexp.type_of() == SEXPTYPE::NILSXP { return Ok(None); }
        let value: &'a mut i32 =
            unsafe { TryFromSexp::try_from_sexp_unchecked(sexp)? };
        Ok(Some(value))
    }
    fn __mx_prepare(sexp: SEXP) -> Prepared<Self> {
        optional_native_scalar::<i32, Self>(sexp, || None)
    }
}
impl<'a> TryFromSexp for Vec<&'a i32> {
    const NATIVE_BORROW: Option<NativeBorrow> =
        NativeBorrow::in_list(<&'a i32 as TryFromSexp>::NATIVE_BORROW);
    type Error = SexpError;
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        map_vecsxp_with(sexp, |_i, elem| TryFromSexp::try_from_sexp(elem))
    }
}
impl<'a> TryFromSexp for Vec<Option<&'a i32>> {
    const NATIVE_BORROW: Option<NativeBorrow> =
        NativeBorrow::in_list(<Option<&'a i32> as
                TryFromSexp>::NATIVE_BORROW);
    type Error = SexpError;
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        map_vecsxp_with(sexp,
            |_i, elem|
                {
                    if elem.type_of() == SEXPTYPE::NILSXP {
                        Ok(None)
                    } else {
                        let value: &'a i32 = TryFromSexp::try_from_sexp(elem)?;
                        Ok(Some(value))
                    }
                })
    }
}
impl<'a> TryFromSexp for Vec<&'a mut i32> {
    const NATIVE_BORROW: Option<NativeBorrow> =
        NativeBorrow::in_list(<&'a mut i32 as TryFromSexp>::NATIVE_BORROW);
    type Error = SexpError;
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        map_mutable_list::<i32,
                _>(sexp, true, false,
            |range| { unsafe { &mut *range.unwrap().data } })
    }
}
impl<'a> TryFromSexp for Vec<Option<&'a mut i32>> {
    const NATIVE_BORROW: Option<NativeBorrow> =
        NativeBorrow::in_list(<Option<&'a mut i32> as
                TryFromSexp>::NATIVE_BORROW);
    type Error = SexpError;
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        map_mutable_list::<i32,
                _>(sexp, true, true,
            |range| { range.map(|range| unsafe { &mut *range.data }) })
    }
}
impl<'a> TryFromSexp for Vec<&'a [i32]> {
    const NATIVE_BORROW: Option<NativeBorrow> =
        NativeBorrow::in_list(<&'a [i32] as TryFromSexp>::NATIVE_BORROW);
    type Error = SexpError;
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        map_vecsxp_with(sexp,
            |_i, elem|
                { TryFromSexp::try_from_sexp(elem).map_err(SexpError::from) })
    }
}
impl<'a> TryFromSexp for Vec<Option<&'a [i32]>> {
    const NATIVE_BORROW: Option<NativeBorrow> =
        NativeBorrow::in_list(<Option<&'a [i32]> as
                TryFromSexp>::NATIVE_BORROW);
    type Error = SexpError;
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        map_vecsxp_with(sexp,
            |_i, elem|
                {
                    if elem.type_of() == SEXPTYPE::NILSXP {
                        Ok(None)
                    } else {
                        let slice: &'a [i32] =
                            TryFromSexp::try_from_sexp(elem).map_err(SexpError::from)?;
                        Ok(Some(slice))
                    }
                })
    }
}
impl<'a> TryFromSexp for Vec<&'a mut [i32]> {
    const NATIVE_BORROW: Option<NativeBorrow> =
        NativeBorrow::in_list(<&'a mut [i32] as TryFromSexp>::NATIVE_BORROW);
    type Error = SexpError;
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        map_mutable_list::<i32,
                _>(sexp, false, false,
            |range|
                {
                    let range = range.unwrap();
                    unsafe { r_slice_mut(range.data, range.len) }
                })
    }
}
impl<'a> TryFromSexp for Vec<Option<&'a mut [i32]>> {
    const NATIVE_BORROW: Option<NativeBorrow> =
        NativeBorrow::in_list(<Option<&'a mut [i32]> as
                TryFromSexp>::NATIVE_BORROW);
    type Error = SexpError;
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        map_mutable_list::<i32,
                _>(sexp, false, true,
            |range|
                {
                    range.map(|range|
                            unsafe { r_slice_mut(range.data, range.len) })
                })
    }
}impl_ref_conversions_for!(i32);
390impl<'a> TryFromSexp for &'a f64 {
    const NATIVE_BORROW: Option<NativeBorrow> =
        Some(NativeBorrow::scalar(<f64 as RNativeType>::SEXP_TYPE, false));
    type Error = SexpError;
    #[inline]
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        native_scalar::<f64>(sexp, || sexp.len())?;
        unsafe {
                    sexp.as_slice::<f64>()
                }.first().ok_or_else(||
                { SexpLengthError { expected: 1, actual: 0 }.into() })
    }
    #[inline]
    unsafe fn try_from_sexp_unchecked(sexp: SEXP)
        -> Result<Self, Self::Error> {
        native_scalar::<f64>(sexp, || unsafe { sexp.len_unchecked() })?;
        unsafe {
                    sexp.as_slice_unchecked::<f64>()
                }.first().ok_or_else(||
                { SexpLengthError { expected: 1, actual: 0 }.into() })
    }
    fn __mx_prepare(sexp: SEXP) -> Prepared<Self> {
        native_leaf(native_scalar::<f64>(sexp, || sexp.len()))
    }
}
/// # Safety note (aliasing)
///
/// This impl can produce aliased `&mut` references if the same R object
/// is passed to multiple mutable parameters. The caller (generated wrapper)
/// rejects overlapping native mutable/shared borrows before conversion.
/// Direct conversion callers must enforce this borrowing contract themselves.
impl<'a> TryFromSexp for &'a mut f64 {
    const NATIVE_BORROW: Option<NativeBorrow> =
        Some(NativeBorrow::scalar(<f64 as RNativeType>::SEXP_TYPE, true));
    type Error = SexpError;
    #[inline]
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        native_scalar::<f64>(sexp, || sexp.len())?;
        let ptr = unsafe { <f64 as RNativeType>::dataptr_mut(sexp) };
        Ok(unsafe { &mut *ptr })
    }
    #[inline]
    unsafe fn try_from_sexp_unchecked(sexp: SEXP)
        -> Result<Self, Self::Error> {
        native_scalar::<f64>(sexp, || unsafe { sexp.len_unchecked() })?;
        let ptr = unsafe { <f64 as RNativeType>::dataptr_mut(sexp) };
        Ok(unsafe { &mut *ptr })
    }
    fn __mx_prepare(sexp: SEXP) -> Prepared<Self> {
        native_leaf(native_scalar::<f64>(sexp, || sexp.len()))
    }
}
impl<'a> TryFromSexp for Option<&'a f64> {
    const NATIVE_BORROW: Option<NativeBorrow> =
        <&'a f64 as TryFromSexp>::NATIVE_BORROW;
    type Error = SexpError;
    #[inline]
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        if sexp.type_of() == SEXPTYPE::NILSXP { return Ok(None); }
        let value: &'a f64 = TryFromSexp::try_from_sexp(sexp)?;
        Ok(Some(value))
    }
    #[inline]
    unsafe fn try_from_sexp_unchecked(sexp: SEXP)
        -> Result<Self, Self::Error> {
        if sexp.type_of() == SEXPTYPE::NILSXP { return Ok(None); }
        let value: &'a f64 =
            unsafe { TryFromSexp::try_from_sexp_unchecked(sexp)? };
        Ok(Some(value))
    }
    fn __mx_prepare(sexp: SEXP) -> Prepared<Self> {
        optional_native_scalar::<f64, Self>(sexp, || None)
    }
}
impl<'a> TryFromSexp for Option<&'a mut f64> {
    const NATIVE_BORROW: Option<NativeBorrow> =
        <&'a mut f64 as TryFromSexp>::NATIVE_BORROW;
    type Error = SexpError;
    #[inline]
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        if sexp.type_of() == SEXPTYPE::NILSXP { return Ok(None); }
        let value: &'a mut f64 = TryFromSexp::try_from_sexp(sexp)?;
        Ok(Some(value))
    }
    #[inline]
    unsafe fn try_from_sexp_unchecked(sexp: SEXP)
        -> Result<Self, Self::Error> {
        if sexp.type_of() == SEXPTYPE::NILSXP { return Ok(None); }
        let value: &'a mut f64 =
            unsafe { TryFromSexp::try_from_sexp_unchecked(sexp)? };
        Ok(Some(value))
    }
    fn __mx_prepare(sexp: SEXP) -> Prepared<Self> {
        optional_native_scalar::<f64, Self>(sexp, || None)
    }
}
impl<'a> TryFromSexp for Vec<&'a f64> {
    const NATIVE_BORROW: Option<NativeBorrow> =
        NativeBorrow::in_list(<&'a f64 as TryFromSexp>::NATIVE_BORROW);
    type Error = SexpError;
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        map_vecsxp_with(sexp, |_i, elem| TryFromSexp::try_from_sexp(elem))
    }
}
impl<'a> TryFromSexp for Vec<Option<&'a f64>> {
    const NATIVE_BORROW: Option<NativeBorrow> =
        NativeBorrow::in_list(<Option<&'a f64> as
                TryFromSexp>::NATIVE_BORROW);
    type Error = SexpError;
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        map_vecsxp_with(sexp,
            |_i, elem|
                {
                    if elem.type_of() == SEXPTYPE::NILSXP {
                        Ok(None)
                    } else {
                        let value: &'a f64 = TryFromSexp::try_from_sexp(elem)?;
                        Ok(Some(value))
                    }
                })
    }
}
impl<'a> TryFromSexp for Vec<&'a mut f64> {
    const NATIVE_BORROW: Option<NativeBorrow> =
        NativeBorrow::in_list(<&'a mut f64 as TryFromSexp>::NATIVE_BORROW);
    type Error = SexpError;
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        map_mutable_list::<f64,
                _>(sexp, true, false,
            |range| { unsafe { &mut *range.unwrap().data } })
    }
}
impl<'a> TryFromSexp for Vec<Option<&'a mut f64>> {
    const NATIVE_BORROW: Option<NativeBorrow> =
        NativeBorrow::in_list(<Option<&'a mut f64> as
                TryFromSexp>::NATIVE_BORROW);
    type Error = SexpError;
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        map_mutable_list::<f64,
                _>(sexp, true, true,
            |range| { range.map(|range| unsafe { &mut *range.data }) })
    }
}
impl<'a> TryFromSexp for Vec<&'a [f64]> {
    const NATIVE_BORROW: Option<NativeBorrow> =
        NativeBorrow::in_list(<&'a [f64] as TryFromSexp>::NATIVE_BORROW);
    type Error = SexpError;
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        map_vecsxp_with(sexp,
            |_i, elem|
                { TryFromSexp::try_from_sexp(elem).map_err(SexpError::from) })
    }
}
impl<'a> TryFromSexp for Vec<Option<&'a [f64]>> {
    const NATIVE_BORROW: Option<NativeBorrow> =
        NativeBorrow::in_list(<Option<&'a [f64]> as
                TryFromSexp>::NATIVE_BORROW);
    type Error = SexpError;
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        map_vecsxp_with(sexp,
            |_i, elem|
                {
                    if elem.type_of() == SEXPTYPE::NILSXP {
                        Ok(None)
                    } else {
                        let slice: &'a [f64] =
                            TryFromSexp::try_from_sexp(elem).map_err(SexpError::from)?;
                        Ok(Some(slice))
                    }
                })
    }
}
impl<'a> TryFromSexp for Vec<&'a mut [f64]> {
    const NATIVE_BORROW: Option<NativeBorrow> =
        NativeBorrow::in_list(<&'a mut [f64] as TryFromSexp>::NATIVE_BORROW);
    type Error = SexpError;
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        map_mutable_list::<f64,
                _>(sexp, false, false,
            |range|
                {
                    let range = range.unwrap();
                    unsafe { r_slice_mut(range.data, range.len) }
                })
    }
}
impl<'a> TryFromSexp for Vec<Option<&'a mut [f64]>> {
    const NATIVE_BORROW: Option<NativeBorrow> =
        NativeBorrow::in_list(<Option<&'a mut [f64]> as
                TryFromSexp>::NATIVE_BORROW);
    type Error = SexpError;
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        map_mutable_list::<f64,
                _>(sexp, false, true,
            |range|
                {
                    range.map(|range|
                            unsafe { r_slice_mut(range.data, range.len) })
                })
    }
}impl_ref_conversions_for!(f64);
391impl<'a> TryFromSexp for &'a u8 {
    const NATIVE_BORROW: Option<NativeBorrow> =
        Some(NativeBorrow::scalar(<u8 as RNativeType>::SEXP_TYPE, false));
    type Error = SexpError;
    #[inline]
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        native_scalar::<u8>(sexp, || sexp.len())?;
        unsafe {
                    sexp.as_slice::<u8>()
                }.first().ok_or_else(||
                { SexpLengthError { expected: 1, actual: 0 }.into() })
    }
    #[inline]
    unsafe fn try_from_sexp_unchecked(sexp: SEXP)
        -> Result<Self, Self::Error> {
        native_scalar::<u8>(sexp, || unsafe { sexp.len_unchecked() })?;
        unsafe {
                    sexp.as_slice_unchecked::<u8>()
                }.first().ok_or_else(||
                { SexpLengthError { expected: 1, actual: 0 }.into() })
    }
    fn __mx_prepare(sexp: SEXP) -> Prepared<Self> {
        native_leaf(native_scalar::<u8>(sexp, || sexp.len()))
    }
}
/// # Safety note (aliasing)
///
/// This impl can produce aliased `&mut` references if the same R object
/// is passed to multiple mutable parameters. The caller (generated wrapper)
/// rejects overlapping native mutable/shared borrows before conversion.
/// Direct conversion callers must enforce this borrowing contract themselves.
impl<'a> TryFromSexp for &'a mut u8 {
    const NATIVE_BORROW: Option<NativeBorrow> =
        Some(NativeBorrow::scalar(<u8 as RNativeType>::SEXP_TYPE, true));
    type Error = SexpError;
    #[inline]
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        native_scalar::<u8>(sexp, || sexp.len())?;
        let ptr = unsafe { <u8 as RNativeType>::dataptr_mut(sexp) };
        Ok(unsafe { &mut *ptr })
    }
    #[inline]
    unsafe fn try_from_sexp_unchecked(sexp: SEXP)
        -> Result<Self, Self::Error> {
        native_scalar::<u8>(sexp, || unsafe { sexp.len_unchecked() })?;
        let ptr = unsafe { <u8 as RNativeType>::dataptr_mut(sexp) };
        Ok(unsafe { &mut *ptr })
    }
    fn __mx_prepare(sexp: SEXP) -> Prepared<Self> {
        native_leaf(native_scalar::<u8>(sexp, || sexp.len()))
    }
}
impl<'a> TryFromSexp for Option<&'a u8> {
    const NATIVE_BORROW: Option<NativeBorrow> =
        <&'a u8 as TryFromSexp>::NATIVE_BORROW;
    type Error = SexpError;
    #[inline]
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        if sexp.type_of() == SEXPTYPE::NILSXP { return Ok(None); }
        let value: &'a u8 = TryFromSexp::try_from_sexp(sexp)?;
        Ok(Some(value))
    }
    #[inline]
    unsafe fn try_from_sexp_unchecked(sexp: SEXP)
        -> Result<Self, Self::Error> {
        if sexp.type_of() == SEXPTYPE::NILSXP { return Ok(None); }
        let value: &'a u8 =
            unsafe { TryFromSexp::try_from_sexp_unchecked(sexp)? };
        Ok(Some(value))
    }
    fn __mx_prepare(sexp: SEXP) -> Prepared<Self> {
        optional_native_scalar::<u8, Self>(sexp, || None)
    }
}
impl<'a> TryFromSexp for Option<&'a mut u8> {
    const NATIVE_BORROW: Option<NativeBorrow> =
        <&'a mut u8 as TryFromSexp>::NATIVE_BORROW;
    type Error = SexpError;
    #[inline]
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        if sexp.type_of() == SEXPTYPE::NILSXP { return Ok(None); }
        let value: &'a mut u8 = TryFromSexp::try_from_sexp(sexp)?;
        Ok(Some(value))
    }
    #[inline]
    unsafe fn try_from_sexp_unchecked(sexp: SEXP)
        -> Result<Self, Self::Error> {
        if sexp.type_of() == SEXPTYPE::NILSXP { return Ok(None); }
        let value: &'a mut u8 =
            unsafe { TryFromSexp::try_from_sexp_unchecked(sexp)? };
        Ok(Some(value))
    }
    fn __mx_prepare(sexp: SEXP) -> Prepared<Self> {
        optional_native_scalar::<u8, Self>(sexp, || None)
    }
}
impl<'a> TryFromSexp for Vec<&'a u8> {
    const NATIVE_BORROW: Option<NativeBorrow> =
        NativeBorrow::in_list(<&'a u8 as TryFromSexp>::NATIVE_BORROW);
    type Error = SexpError;
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        map_vecsxp_with(sexp, |_i, elem| TryFromSexp::try_from_sexp(elem))
    }
}
impl<'a> TryFromSexp for Vec<Option<&'a u8>> {
    const NATIVE_BORROW: Option<NativeBorrow> =
        NativeBorrow::in_list(<Option<&'a u8> as TryFromSexp>::NATIVE_BORROW);
    type Error = SexpError;
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        map_vecsxp_with(sexp,
            |_i, elem|
                {
                    if elem.type_of() == SEXPTYPE::NILSXP {
                        Ok(None)
                    } else {
                        let value: &'a u8 = TryFromSexp::try_from_sexp(elem)?;
                        Ok(Some(value))
                    }
                })
    }
}
impl<'a> TryFromSexp for Vec<&'a mut u8> {
    const NATIVE_BORROW: Option<NativeBorrow> =
        NativeBorrow::in_list(<&'a mut u8 as TryFromSexp>::NATIVE_BORROW);
    type Error = SexpError;
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        map_mutable_list::<u8,
                _>(sexp, true, false,
            |range| { unsafe { &mut *range.unwrap().data } })
    }
}
impl<'a> TryFromSexp for Vec<Option<&'a mut u8>> {
    const NATIVE_BORROW: Option<NativeBorrow> =
        NativeBorrow::in_list(<Option<&'a mut u8> as
                TryFromSexp>::NATIVE_BORROW);
    type Error = SexpError;
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        map_mutable_list::<u8,
                _>(sexp, true, true,
            |range| { range.map(|range| unsafe { &mut *range.data }) })
    }
}
impl<'a> TryFromSexp for Vec<&'a [u8]> {
    const NATIVE_BORROW: Option<NativeBorrow> =
        NativeBorrow::in_list(<&'a [u8] as TryFromSexp>::NATIVE_BORROW);
    type Error = SexpError;
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        map_vecsxp_with(sexp,
            |_i, elem|
                { TryFromSexp::try_from_sexp(elem).map_err(SexpError::from) })
    }
}
impl<'a> TryFromSexp for Vec<Option<&'a [u8]>> {
    const NATIVE_BORROW: Option<NativeBorrow> =
        NativeBorrow::in_list(<Option<&'a [u8]> as
                TryFromSexp>::NATIVE_BORROW);
    type Error = SexpError;
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        map_vecsxp_with(sexp,
            |_i, elem|
                {
                    if elem.type_of() == SEXPTYPE::NILSXP {
                        Ok(None)
                    } else {
                        let slice: &'a [u8] =
                            TryFromSexp::try_from_sexp(elem).map_err(SexpError::from)?;
                        Ok(Some(slice))
                    }
                })
    }
}
impl<'a> TryFromSexp for Vec<&'a mut [u8]> {
    const NATIVE_BORROW: Option<NativeBorrow> =
        NativeBorrow::in_list(<&'a mut [u8] as TryFromSexp>::NATIVE_BORROW);
    type Error = SexpError;
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        map_mutable_list::<u8,
                _>(sexp, false, false,
            |range|
                {
                    let range = range.unwrap();
                    unsafe { r_slice_mut(range.data, range.len) }
                })
    }
}
impl<'a> TryFromSexp for Vec<Option<&'a mut [u8]>> {
    const NATIVE_BORROW: Option<NativeBorrow> =
        NativeBorrow::in_list(<Option<&'a mut [u8]> as
                TryFromSexp>::NATIVE_BORROW);
    type Error = SexpError;
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        map_mutable_list::<u8,
                _>(sexp, false, true,
            |range|
                {
                    range.map(|range|
                            unsafe { r_slice_mut(range.data, range.len) })
                })
    }
}impl_ref_conversions_for!(u8);
392impl<'a> TryFromSexp for &'a RLogical {
    const NATIVE_BORROW: Option<NativeBorrow> =
        Some(NativeBorrow::scalar(<RLogical as RNativeType>::SEXP_TYPE,
                false));
    type Error = SexpError;
    #[inline]
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        native_scalar::<RLogical>(sexp, || sexp.len())?;
        unsafe {
                    sexp.as_slice::<RLogical>()
                }.first().ok_or_else(||
                { SexpLengthError { expected: 1, actual: 0 }.into() })
    }
    #[inline]
    unsafe fn try_from_sexp_unchecked(sexp: SEXP)
        -> Result<Self, Self::Error> {
        native_scalar::<RLogical>(sexp, || unsafe { sexp.len_unchecked() })?;
        unsafe {
                    sexp.as_slice_unchecked::<RLogical>()
                }.first().ok_or_else(||
                { SexpLengthError { expected: 1, actual: 0 }.into() })
    }
    fn __mx_prepare(sexp: SEXP) -> Prepared<Self> {
        native_leaf(native_scalar::<RLogical>(sexp, || sexp.len()))
    }
}
/// # Safety note (aliasing)
///
/// This impl can produce aliased `&mut` references if the same R object
/// is passed to multiple mutable parameters. The caller (generated wrapper)
/// rejects overlapping native mutable/shared borrows before conversion.
/// Direct conversion callers must enforce this borrowing contract themselves.
impl<'a> TryFromSexp for &'a mut RLogical {
    const NATIVE_BORROW: Option<NativeBorrow> =
        Some(NativeBorrow::scalar(<RLogical as RNativeType>::SEXP_TYPE,
                true));
    type Error = SexpError;
    #[inline]
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        native_scalar::<RLogical>(sexp, || sexp.len())?;
        let ptr = unsafe { <RLogical as RNativeType>::dataptr_mut(sexp) };
        Ok(unsafe { &mut *ptr })
    }
    #[inline]
    unsafe fn try_from_sexp_unchecked(sexp: SEXP)
        -> Result<Self, Self::Error> {
        native_scalar::<RLogical>(sexp, || unsafe { sexp.len_unchecked() })?;
        let ptr = unsafe { <RLogical as RNativeType>::dataptr_mut(sexp) };
        Ok(unsafe { &mut *ptr })
    }
    fn __mx_prepare(sexp: SEXP) -> Prepared<Self> {
        native_leaf(native_scalar::<RLogical>(sexp, || sexp.len()))
    }
}
impl<'a> TryFromSexp for Option<&'a RLogical> {
    const NATIVE_BORROW: Option<NativeBorrow> =
        <&'a RLogical as TryFromSexp>::NATIVE_BORROW;
    type Error = SexpError;
    #[inline]
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        if sexp.type_of() == SEXPTYPE::NILSXP { return Ok(None); }
        let value: &'a RLogical = TryFromSexp::try_from_sexp(sexp)?;
        Ok(Some(value))
    }
    #[inline]
    unsafe fn try_from_sexp_unchecked(sexp: SEXP)
        -> Result<Self, Self::Error> {
        if sexp.type_of() == SEXPTYPE::NILSXP { return Ok(None); }
        let value: &'a RLogical =
            unsafe { TryFromSexp::try_from_sexp_unchecked(sexp)? };
        Ok(Some(value))
    }
    fn __mx_prepare(sexp: SEXP) -> Prepared<Self> {
        optional_native_scalar::<RLogical, Self>(sexp, || None)
    }
}
impl<'a> TryFromSexp for Option<&'a mut RLogical> {
    const NATIVE_BORROW: Option<NativeBorrow> =
        <&'a mut RLogical as TryFromSexp>::NATIVE_BORROW;
    type Error = SexpError;
    #[inline]
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        if sexp.type_of() == SEXPTYPE::NILSXP { return Ok(None); }
        let value: &'a mut RLogical = TryFromSexp::try_from_sexp(sexp)?;
        Ok(Some(value))
    }
    #[inline]
    unsafe fn try_from_sexp_unchecked(sexp: SEXP)
        -> Result<Self, Self::Error> {
        if sexp.type_of() == SEXPTYPE::NILSXP { return Ok(None); }
        let value: &'a mut RLogical =
            unsafe { TryFromSexp::try_from_sexp_unchecked(sexp)? };
        Ok(Some(value))
    }
    fn __mx_prepare(sexp: SEXP) -> Prepared<Self> {
        optional_native_scalar::<RLogical, Self>(sexp, || None)
    }
}
impl<'a> TryFromSexp for Vec<&'a RLogical> {
    const NATIVE_BORROW: Option<NativeBorrow> =
        NativeBorrow::in_list(<&'a RLogical as TryFromSexp>::NATIVE_BORROW);
    type Error = SexpError;
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        map_vecsxp_with(sexp, |_i, elem| TryFromSexp::try_from_sexp(elem))
    }
}
impl<'a> TryFromSexp for Vec<Option<&'a RLogical>> {
    const NATIVE_BORROW: Option<NativeBorrow> =
        NativeBorrow::in_list(<Option<&'a RLogical> as
                TryFromSexp>::NATIVE_BORROW);
    type Error = SexpError;
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        map_vecsxp_with(sexp,
            |_i, elem|
                {
                    if elem.type_of() == SEXPTYPE::NILSXP {
                        Ok(None)
                    } else {
                        let value: &'a RLogical = TryFromSexp::try_from_sexp(elem)?;
                        Ok(Some(value))
                    }
                })
    }
}
impl<'a> TryFromSexp for Vec<&'a mut RLogical> {
    const NATIVE_BORROW: Option<NativeBorrow> =
        NativeBorrow::in_list(<&'a mut RLogical as
                TryFromSexp>::NATIVE_BORROW);
    type Error = SexpError;
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        map_mutable_list::<RLogical,
                _>(sexp, true, false,
            |range| { unsafe { &mut *range.unwrap().data } })
    }
}
impl<'a> TryFromSexp for Vec<Option<&'a mut RLogical>> {
    const NATIVE_BORROW: Option<NativeBorrow> =
        NativeBorrow::in_list(<Option<&'a mut RLogical> as
                TryFromSexp>::NATIVE_BORROW);
    type Error = SexpError;
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        map_mutable_list::<RLogical,
                _>(sexp, true, true,
            |range| { range.map(|range| unsafe { &mut *range.data }) })
    }
}
impl<'a> TryFromSexp for Vec<&'a [RLogical]> {
    const NATIVE_BORROW: Option<NativeBorrow> =
        NativeBorrow::in_list(<&'a [RLogical] as TryFromSexp>::NATIVE_BORROW);
    type Error = SexpError;
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        map_vecsxp_with(sexp,
            |_i, elem|
                { TryFromSexp::try_from_sexp(elem).map_err(SexpError::from) })
    }
}
impl<'a> TryFromSexp for Vec<Option<&'a [RLogical]>> {
    const NATIVE_BORROW: Option<NativeBorrow> =
        NativeBorrow::in_list(<Option<&'a [RLogical]> as
                TryFromSexp>::NATIVE_BORROW);
    type Error = SexpError;
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        map_vecsxp_with(sexp,
            |_i, elem|
                {
                    if elem.type_of() == SEXPTYPE::NILSXP {
                        Ok(None)
                    } else {
                        let slice: &'a [RLogical] =
                            TryFromSexp::try_from_sexp(elem).map_err(SexpError::from)?;
                        Ok(Some(slice))
                    }
                })
    }
}
impl<'a> TryFromSexp for Vec<&'a mut [RLogical]> {
    const NATIVE_BORROW: Option<NativeBorrow> =
        NativeBorrow::in_list(<&'a mut [RLogical] as
                TryFromSexp>::NATIVE_BORROW);
    type Error = SexpError;
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        map_mutable_list::<RLogical,
                _>(sexp, false, false,
            |range|
                {
                    let range = range.unwrap();
                    unsafe { r_slice_mut(range.data, range.len) }
                })
    }
}
impl<'a> TryFromSexp for Vec<Option<&'a mut [RLogical]>> {
    const NATIVE_BORROW: Option<NativeBorrow> =
        NativeBorrow::in_list(<Option<&'a mut [RLogical]> as
                TryFromSexp>::NATIVE_BORROW);
    type Error = SexpError;
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        map_mutable_list::<RLogical,
                _>(sexp, false, true,
            |range|
                {
                    range.map(|range|
                            unsafe { r_slice_mut(range.data, range.len) })
                })
    }
}impl_ref_conversions_for!(RLogical);
393impl<'a> TryFromSexp for &'a crate::Rcomplex {
    const NATIVE_BORROW: Option<NativeBorrow> =
        Some(NativeBorrow::scalar(<crate::Rcomplex as RNativeType>::SEXP_TYPE,
                false));
    type Error = SexpError;
    #[inline]
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        native_scalar::<crate::Rcomplex>(sexp, || sexp.len())?;
        unsafe {
                    sexp.as_slice::<crate::Rcomplex>()
                }.first().ok_or_else(||
                { SexpLengthError { expected: 1, actual: 0 }.into() })
    }
    #[inline]
    unsafe fn try_from_sexp_unchecked(sexp: SEXP)
        -> Result<Self, Self::Error> {
        native_scalar::<crate::Rcomplex>(sexp,
                || unsafe { sexp.len_unchecked() })?;
        unsafe {
                    sexp.as_slice_unchecked::<crate::Rcomplex>()
                }.first().ok_or_else(||
                { SexpLengthError { expected: 1, actual: 0 }.into() })
    }
    fn __mx_prepare(sexp: SEXP) -> Prepared<Self> {
        native_leaf(native_scalar::<crate::Rcomplex>(sexp, || sexp.len()))
    }
}
/// # Safety note (aliasing)
///
/// This impl can produce aliased `&mut` references if the same R object
/// is passed to multiple mutable parameters. The caller (generated wrapper)
/// rejects overlapping native mutable/shared borrows before conversion.
/// Direct conversion callers must enforce this borrowing contract themselves.
impl<'a> TryFromSexp for &'a mut crate::Rcomplex {
    const NATIVE_BORROW: Option<NativeBorrow> =
        Some(NativeBorrow::scalar(<crate::Rcomplex as RNativeType>::SEXP_TYPE,
                true));
    type Error = SexpError;
    #[inline]
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        native_scalar::<crate::Rcomplex>(sexp, || sexp.len())?;
        let ptr =
            unsafe { <crate::Rcomplex as RNativeType>::dataptr_mut(sexp) };
        Ok(unsafe { &mut *ptr })
    }
    #[inline]
    unsafe fn try_from_sexp_unchecked(sexp: SEXP)
        -> Result<Self, Self::Error> {
        native_scalar::<crate::Rcomplex>(sexp,
                || unsafe { sexp.len_unchecked() })?;
        let ptr =
            unsafe { <crate::Rcomplex as RNativeType>::dataptr_mut(sexp) };
        Ok(unsafe { &mut *ptr })
    }
    fn __mx_prepare(sexp: SEXP) -> Prepared<Self> {
        native_leaf(native_scalar::<crate::Rcomplex>(sexp, || sexp.len()))
    }
}
impl<'a> TryFromSexp for Option<&'a crate::Rcomplex> {
    const NATIVE_BORROW: Option<NativeBorrow> =
        <&'a crate::Rcomplex as TryFromSexp>::NATIVE_BORROW;
    type Error = SexpError;
    #[inline]
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        if sexp.type_of() == SEXPTYPE::NILSXP { return Ok(None); }
        let value: &'a crate::Rcomplex = TryFromSexp::try_from_sexp(sexp)?;
        Ok(Some(value))
    }
    #[inline]
    unsafe fn try_from_sexp_unchecked(sexp: SEXP)
        -> Result<Self, Self::Error> {
        if sexp.type_of() == SEXPTYPE::NILSXP { return Ok(None); }
        let value: &'a crate::Rcomplex =
            unsafe { TryFromSexp::try_from_sexp_unchecked(sexp)? };
        Ok(Some(value))
    }
    fn __mx_prepare(sexp: SEXP) -> Prepared<Self> {
        optional_native_scalar::<crate::Rcomplex, Self>(sexp, || None)
    }
}
impl<'a> TryFromSexp for Option<&'a mut crate::Rcomplex> {
    const NATIVE_BORROW: Option<NativeBorrow> =
        <&'a mut crate::Rcomplex as TryFromSexp>::NATIVE_BORROW;
    type Error = SexpError;
    #[inline]
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        if sexp.type_of() == SEXPTYPE::NILSXP { return Ok(None); }
        let value: &'a mut crate::Rcomplex =
            TryFromSexp::try_from_sexp(sexp)?;
        Ok(Some(value))
    }
    #[inline]
    unsafe fn try_from_sexp_unchecked(sexp: SEXP)
        -> Result<Self, Self::Error> {
        if sexp.type_of() == SEXPTYPE::NILSXP { return Ok(None); }
        let value: &'a mut crate::Rcomplex =
            unsafe { TryFromSexp::try_from_sexp_unchecked(sexp)? };
        Ok(Some(value))
    }
    fn __mx_prepare(sexp: SEXP) -> Prepared<Self> {
        optional_native_scalar::<crate::Rcomplex, Self>(sexp, || None)
    }
}
impl<'a> TryFromSexp for Vec<&'a crate::Rcomplex> {
    const NATIVE_BORROW: Option<NativeBorrow> =
        NativeBorrow::in_list(<&'a crate::Rcomplex as
                TryFromSexp>::NATIVE_BORROW);
    type Error = SexpError;
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        map_vecsxp_with(sexp, |_i, elem| TryFromSexp::try_from_sexp(elem))
    }
}
impl<'a> TryFromSexp for Vec<Option<&'a crate::Rcomplex>> {
    const NATIVE_BORROW: Option<NativeBorrow> =
        NativeBorrow::in_list(<Option<&'a crate::Rcomplex> as
                TryFromSexp>::NATIVE_BORROW);
    type Error = SexpError;
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        map_vecsxp_with(sexp,
            |_i, elem|
                {
                    if elem.type_of() == SEXPTYPE::NILSXP {
                        Ok(None)
                    } else {
                        let value: &'a crate::Rcomplex =
                            TryFromSexp::try_from_sexp(elem)?;
                        Ok(Some(value))
                    }
                })
    }
}
impl<'a> TryFromSexp for Vec<&'a mut crate::Rcomplex> {
    const NATIVE_BORROW: Option<NativeBorrow> =
        NativeBorrow::in_list(<&'a mut crate::Rcomplex as
                TryFromSexp>::NATIVE_BORROW);
    type Error = SexpError;
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        map_mutable_list::<crate::Rcomplex,
                _>(sexp, true, false,
            |range| { unsafe { &mut *range.unwrap().data } })
    }
}
impl<'a> TryFromSexp for Vec<Option<&'a mut crate::Rcomplex>> {
    const NATIVE_BORROW: Option<NativeBorrow> =
        NativeBorrow::in_list(<Option<&'a mut crate::Rcomplex> as
                TryFromSexp>::NATIVE_BORROW);
    type Error = SexpError;
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        map_mutable_list::<crate::Rcomplex,
                _>(sexp, true, true,
            |range| { range.map(|range| unsafe { &mut *range.data }) })
    }
}
impl<'a> TryFromSexp for Vec<&'a [crate::Rcomplex]> {
    const NATIVE_BORROW: Option<NativeBorrow> =
        NativeBorrow::in_list(<&'a [crate::Rcomplex] as
                TryFromSexp>::NATIVE_BORROW);
    type Error = SexpError;
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        map_vecsxp_with(sexp,
            |_i, elem|
                { TryFromSexp::try_from_sexp(elem).map_err(SexpError::from) })
    }
}
impl<'a> TryFromSexp for Vec<Option<&'a [crate::Rcomplex]>> {
    const NATIVE_BORROW: Option<NativeBorrow> =
        NativeBorrow::in_list(<Option<&'a [crate::Rcomplex]> as
                TryFromSexp>::NATIVE_BORROW);
    type Error = SexpError;
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        map_vecsxp_with(sexp,
            |_i, elem|
                {
                    if elem.type_of() == SEXPTYPE::NILSXP {
                        Ok(None)
                    } else {
                        let slice: &'a [crate::Rcomplex] =
                            TryFromSexp::try_from_sexp(elem).map_err(SexpError::from)?;
                        Ok(Some(slice))
                    }
                })
    }
}
impl<'a> TryFromSexp for Vec<&'a mut [crate::Rcomplex]> {
    const NATIVE_BORROW: Option<NativeBorrow> =
        NativeBorrow::in_list(<&'a mut [crate::Rcomplex] as
                TryFromSexp>::NATIVE_BORROW);
    type Error = SexpError;
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        map_mutable_list::<crate::Rcomplex,
                _>(sexp, false, false,
            |range|
                {
                    let range = range.unwrap();
                    unsafe { r_slice_mut(range.data, range.len) }
                })
    }
}
impl<'a> TryFromSexp for Vec<Option<&'a mut [crate::Rcomplex]>> {
    const NATIVE_BORROW: Option<NativeBorrow> =
        NativeBorrow::in_list(<Option<&'a mut [crate::Rcomplex]> as
                TryFromSexp>::NATIVE_BORROW);
    type Error = SexpError;
    fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error> {
        map_mutable_list::<crate::Rcomplex,
                _>(sexp, false, true,
            |range|
                {
                    range.map(|range|
                            unsafe { r_slice_mut(range.data, range.len) })
                })
    }
}impl_ref_conversions_for!(crate::Rcomplex);
394// endregion