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