1macro_rules! impl_option_into_r_null {
82 ($t:ty) => {
83 impl $crate::into_r::IntoR for Option<$t> {
84 type Error = <$t as $crate::into_r::IntoR>::Error;
85 #[inline]
86 fn try_into_sexp(self) -> Result<$crate::SEXP, Self::Error> {
87 match self {
88 Some(v) => v.try_into_sexp(),
89 None => Ok($crate::SEXP::nil()),
90 }
91 }
92 #[inline]
93 unsafe fn try_into_sexp_unchecked(self) -> Result<$crate::SEXP, Self::Error> {
94 match self {
95 Some(v) => unsafe { v.try_into_sexp_unchecked() },
96 None => Ok($crate::SEXP::nil()),
97 }
98 }
99 #[inline]
100 fn into_sexp(self) -> $crate::SEXP {
101 match self {
102 Some(v) => v.into_sexp(),
103 None => $crate::SEXP::nil(),
104 }
105 }
106 #[inline]
107 unsafe fn into_sexp_unchecked(self) -> $crate::SEXP {
108 match self {
109 Some(v) => unsafe { v.into_sexp_unchecked() },
110 None => $crate::SEXP::nil(),
111 }
112 }
113 }
114 };
115}
116
117mod as_character;
118mod as_numeric;
119pub(crate) use as_character::read_character;
120pub use as_character::{AsCharacter, AsCharacterVec};
121pub use as_numeric::{AsNumeric, AsNumericVec};
122
123use crate::RNativeType;
124use crate::externalptr::{ExternalPtr, IntoExternalPtr};
125use crate::into_r::IntoR;
126use crate::list::{IntoList, List};
127use crate::named_vector::AtomicElement;
128
129#[derive(#[automatically_derived]
impl<T: ::core::fmt::Debug + IntoList> ::core::fmt::Debug for AsList<T> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_tuple_field1_finish(f, "AsList",
&&self.0)
}
}Debug, #[automatically_derived]
impl<T: ::core::clone::Clone + IntoList> ::core::clone::Clone for AsList<T> {
#[inline]
fn clone(&self) -> Self { AsList(::core::clone::Clone::clone(&self.0)) }
}Clone, #[automatically_derived]
impl<T: ::core::marker::Copy + IntoList> ::core::marker::Copy for AsList<T> {
}Copy)]
147pub struct AsList<T: IntoList>(pub T);
148
149impl<T: IntoList> From<T> for AsList<T> {
150 fn from(value: T) -> Self {
151 AsList(value)
152 }
153}
154
155impl<T: IntoList> IntoR for AsList<T> {
156 type Error = std::convert::Infallible;
157
158 #[inline]
159 fn try_into_sexp(self) -> Result<crate::SEXP, Self::Error> {
160 Ok(self.into_sexp())
161 }
162
163 #[inline]
164 unsafe fn try_into_sexp_unchecked(self) -> Result<crate::SEXP, Self::Error> {
165 self.try_into_sexp()
166 }
167
168 #[inline]
169 fn into_sexp(self) -> crate::SEXP {
170 self.0.into_list().into_sexp()
171 }
172}
173
174pub use crate::dataframe::ColumnSource;
180
181#[doc(hidden)]
215pub unsafe fn scatter_column(
216 src: crate::SEXP,
217 present_idx: &[usize],
218 n_rows: usize,
219) -> crate::SEXP {
220 #[allow(unused_unsafe)]
222 unsafe {
223 use crate::{RLogical, Rcomplex, SEXPTYPE, SexpExt as _};
224
225 let out = match src.type_of() {
226 SEXPTYPE::REALSXP => scatter_native::<f64>(src, present_idx, n_rows),
227 SEXPTYPE::INTSXP => scatter_native::<i32>(src, present_idx, n_rows),
228 SEXPTYPE::LGLSXP => scatter_native::<RLogical>(src, present_idx, n_rows),
229 SEXPTYPE::RAWSXP => scatter_native::<u8>(src, present_idx, n_rows),
230 SEXPTYPE::CPLXSXP => scatter_native::<Rcomplex>(src, present_idx, n_rows),
231 SEXPTYPE::STRSXP => {
232 let out = crate::sys::Rf_allocVector(SEXPTYPE::STRSXP, n_rows as isize);
233 for i in 0..(n_rows as isize) {
236 out.set_string_elt(i, crate::SEXP::na_string());
237 }
238 for (pi, &row_i) in present_idx.iter().enumerate() {
239 out.set_string_elt(row_i as isize, src.string_elt(pi as isize));
240 }
241 out
242 }
243 SEXPTYPE::VECSXP => {
244 let out = crate::sys::Rf_allocVector(SEXPTYPE::VECSXP, n_rows as isize);
245 for (pi, &row_i) in present_idx.iter().enumerate() {
248 out.set_vector_elt(row_i as isize, src.vector_elt(pi as isize));
249 }
250 out
251 }
252 _ => {
253 let out = crate::sys::Rf_allocVector(SEXPTYPE::VECSXP, n_rows as isize);
256 for (pi, &row_i) in present_idx.iter().enumerate() {
257 out.set_vector_elt(row_i as isize, src.vector_elt(pi as isize));
258 }
259 return out;
260 }
261 };
262 let out = crate::OwnedProtect::new(out);
264 out.copy_most_attrib(src);
265 *out
266 }
267}
268
269#[inline]
287unsafe fn scatter_native<T: RNativeType + Copy>(
288 src: crate::SEXP,
289 present_idx: &[usize],
290 n_rows: usize,
291) -> crate::SEXP {
292 unsafe {
293 use crate::SexpExt as _;
294 let src_vals: &[T] = src.as_slice::<T>();
295 let out = crate::sys::Rf_allocVector(T::SEXP_TYPE, n_rows as isize);
296 let out_vals: &mut [T] = out.as_mut_slice::<T>();
297 out_vals.fill(T::R_NA);
300 for (pi, &row_i) in present_idx.iter().enumerate() {
301 out_vals[row_i] = src_vals[pi];
302 }
303 out
304 }
305}
306
307#[inline]
322unsafe fn gather_native<T: RNativeType + Copy>(src: crate::SEXP, idx: &[usize]) -> crate::SEXP {
323 unsafe {
324 use crate::SexpExt as _;
325 let src_vals: &[T] = src.as_slice::<T>();
326 let out = crate::sys::Rf_allocVector(T::SEXP_TYPE, idx.len() as isize);
327 let out_vals: &mut [T] = out.as_mut_slice::<T>();
328 for (dst, &row_i) in out_vals.iter_mut().zip(idx) {
329 *dst = src_vals[row_i];
330 }
331 out
332 }
333}
334
335#[doc(hidden)]
363pub unsafe fn gather_column(src: crate::SEXP, idx: &[usize]) -> crate::SEXP {
364 #[allow(unused_unsafe)]
366 unsafe {
367 use crate::{RLogical, Rcomplex, SEXPTYPE, SexpExt as _};
368
369 match src.type_of() {
370 SEXPTYPE::REALSXP => gather_native::<f64>(src, idx),
371 SEXPTYPE::INTSXP => gather_native::<i32>(src, idx),
372 SEXPTYPE::LGLSXP => gather_native::<RLogical>(src, idx),
373 SEXPTYPE::RAWSXP => gather_native::<u8>(src, idx),
374 SEXPTYPE::CPLXSXP => gather_native::<Rcomplex>(src, idx),
375 SEXPTYPE::STRSXP => {
376 let out = crate::sys::Rf_allocVector(SEXPTYPE::STRSXP, idx.len() as isize);
378 for (j, &row_i) in idx.iter().enumerate() {
379 out.set_string_elt(j as isize, src.string_elt(row_i as isize));
380 }
381 out
382 }
383 SEXPTYPE::VECSXP => {
384 let out = crate::sys::Rf_allocVector(SEXPTYPE::VECSXP, idx.len() as isize);
386 for (j, &row_i) in idx.iter().enumerate() {
387 out.set_vector_elt(j as isize, src.vector_elt(row_i as isize));
388 }
389 out
390 }
391 _ => {
392 let out = crate::sys::Rf_allocVector(SEXPTYPE::LGLSXP, idx.len() as isize);
394 let na_vals: &mut [RLogical] = out.as_mut_slice::<RLogical>();
395 na_vals.fill(RLogical::NA);
396 out
397 }
398 }
399 }
400}
401#[derive(#[automatically_derived]
impl<T: ::core::fmt::Debug + IntoExternalPtr> ::core::fmt::Debug for
AsExternalPtr<T> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_tuple_field1_finish(f, "AsExternalPtr",
&&self.0)
}
}Debug, #[automatically_derived]
impl<T: ::core::clone::Clone + IntoExternalPtr> ::core::clone::Clone for
AsExternalPtr<T> {
#[inline]
fn clone(&self) -> Self {
AsExternalPtr(::core::clone::Clone::clone(&self.0))
}
}Clone, #[automatically_derived]
impl<T: ::core::marker::Copy + IntoExternalPtr> ::core::marker::Copy for
AsExternalPtr<T> {
}Copy)]
422pub struct AsExternalPtr<T: IntoExternalPtr>(pub T);
423
424impl<T: IntoExternalPtr> From<T> for AsExternalPtr<T> {
425 fn from(value: T) -> Self {
426 AsExternalPtr(value)
427 }
428}
429
430impl<T: IntoExternalPtr> IntoR for AsExternalPtr<T> {
431 type Error = std::convert::Infallible;
432
433 #[inline]
434 fn try_into_sexp(self) -> Result<crate::SEXP, Self::Error> {
435 Ok(self.into_sexp())
436 }
437
438 #[inline]
439 unsafe fn try_into_sexp_unchecked(self) -> Result<crate::SEXP, Self::Error> {
440 self.try_into_sexp()
441 }
442
443 #[inline]
444 fn into_sexp(self) -> crate::SEXP {
445 ExternalPtr::new(self.0).into_sexp()
446 }
447}
448
449#[derive(#[automatically_derived]
impl<T: ::core::fmt::Debug + RNativeType> ::core::fmt::Debug for AsRNative<T>
{
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_tuple_field1_finish(f, "AsRNative",
&&self.0)
}
}Debug, #[automatically_derived]
impl<T: ::core::clone::Clone + RNativeType> ::core::clone::Clone for
AsRNative<T> {
#[inline]
fn clone(&self) -> Self {
AsRNative(::core::clone::Clone::clone(&self.0))
}
}Clone, #[automatically_derived]
impl<T: ::core::marker::Copy + RNativeType> ::core::marker::Copy for
AsRNative<T> {
}Copy)]
475pub struct AsRNative<T: RNativeType>(pub T);
476
477impl<T: RNativeType> From<T> for AsRNative<T> {
478 fn from(value: T) -> Self {
479 AsRNative(value)
480 }
481}
482
483impl<T: RNativeType> IntoR for AsRNative<T> {
484 type Error = std::convert::Infallible;
485
486 #[inline]
487 fn try_into_sexp(self) -> Result<crate::SEXP, Self::Error> {
488 Ok(self.into_sexp())
489 }
490
491 #[inline]
492 unsafe fn try_into_sexp_unchecked(self) -> Result<crate::SEXP, Self::Error> {
493 Ok(unsafe { self.into_sexp_unchecked() })
494 }
495
496 #[inline]
497 fn into_sexp(self) -> crate::SEXP {
498 unsafe {
501 let sexp = crate::sys::Rf_allocVector(T::SEXP_TYPE, 1);
502 let ptr = T::dataptr_mut(sexp);
503 std::ptr::write(ptr, self.0);
504 sexp
505 }
506 }
507
508 #[inline]
509 unsafe fn into_sexp_unchecked(self) -> crate::SEXP {
510 unsafe {
511 let sexp = crate::sys::Rf_allocVector_unchecked(T::SEXP_TYPE, 1);
512 let ptr = T::dataptr_mut(sexp);
513 std::ptr::write(ptr, self.0);
514 sexp
515 }
516 }
517}
518#[derive(#[automatically_derived]
impl<T: ::core::fmt::Debug + crate::dataframe::IntoDataFrame>
::core::fmt::Debug for AsDataFrame<T> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_tuple_field1_finish(f, "AsDataFrame",
&&self.0)
}
}Debug, #[automatically_derived]
impl<T: ::core::clone::Clone + crate::dataframe::IntoDataFrame>
::core::clone::Clone for AsDataFrame<T> {
#[inline]
fn clone(&self) -> Self {
AsDataFrame(::core::clone::Clone::clone(&self.0))
}
}Clone)]
545pub struct AsDataFrame<T: crate::dataframe::IntoDataFrame>(pub T);
546
547impl<T: crate::dataframe::IntoDataFrame> From<T> for AsDataFrame<T> {
548 fn from(value: T) -> Self {
549 AsDataFrame(value)
550 }
551}
552
553impl<T: crate::dataframe::IntoDataFrame> IntoR for AsDataFrame<T> {
554 type Error = crate::dataframe::DataFrameError;
555
556 #[inline]
557 fn try_into_sexp(self) -> Result<crate::SEXP, Self::Error> {
558 Ok(self.0.into_dataframe()?.into_sexp())
559 }
560
561 #[inline]
562 unsafe fn try_into_sexp_unchecked(self) -> Result<crate::SEXP, Self::Error> {
563 self.try_into_sexp()
564 }
565}
566
567#[cfg(feature = "vctrs")]
590#[derive(Debug, Clone)]
591pub struct AsVctrs<T: crate::vctrs::IntoVctrs>(pub T);
592
593#[cfg(feature = "vctrs")]
594impl<T: crate::vctrs::IntoVctrs> From<T> for AsVctrs<T> {
595 fn from(value: T) -> Self {
596 AsVctrs(value)
597 }
598}
599
600#[cfg(feature = "vctrs")]
601impl<T: crate::vctrs::IntoVctrs> IntoR for AsVctrs<T> {
602 type Error = crate::vctrs::VctrsBuildError;
603
604 #[inline]
605 fn try_into_sexp(self) -> Result<crate::SEXP, Self::Error> {
606 self.0.into_vctrs()
607 }
608
609 #[inline]
610 unsafe fn try_into_sexp_unchecked(self) -> Result<crate::SEXP, Self::Error> {
611 self.try_into_sexp()
612 }
613}
614#[derive(#[automatically_derived]
impl<T: ::core::fmt::Debug> ::core::fmt::Debug for AsNamedList<T> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_tuple_field1_finish(f, "AsNamedList",
&&self.0)
}
}Debug, #[automatically_derived]
impl<T: ::core::clone::Clone> ::core::clone::Clone for AsNamedList<T> {
#[inline]
fn clone(&self) -> Self {
AsNamedList(::core::clone::Clone::clone(&self.0))
}
}Clone)]
664pub struct AsNamedList<T>(pub T);
665
666impl<T> From<T> for AsNamedList<T> {
667 fn from(value: T) -> Self {
668 AsNamedList(value)
669 }
670}
671
672impl<K: AsRef<str>, V: IntoR> IntoR for AsNamedList<Vec<(K, V)>> {
673 type Error = std::convert::Infallible;
674
675 fn try_into_sexp(self) -> Result<crate::SEXP, Self::Error> {
676 Ok(self.into_sexp())
677 }
678
679 unsafe fn try_into_sexp_unchecked(self) -> Result<crate::SEXP, Self::Error> {
680 self.try_into_sexp()
681 }
682
683 fn into_sexp(self) -> crate::SEXP {
684 unsafe {
690 let scope = crate::gc_protect::ProtectScope::new();
691 let pairs: Vec<(K, crate::SEXP)> = self
692 .0
693 .into_iter()
694 .map(|(k, v)| (k, scope.protect_raw(v.into_sexp())))
695 .collect();
696 List::from_raw_pairs(pairs).into_sexp()
697 }
698 }
699}
700
701impl<K: AsRef<str>, V: IntoR, const N: usize> IntoR for AsNamedList<[(K, V); N]> {
702 type Error = std::convert::Infallible;
703
704 fn try_into_sexp(self) -> Result<crate::SEXP, Self::Error> {
705 Ok(self.into_sexp())
706 }
707
708 unsafe fn try_into_sexp_unchecked(self) -> Result<crate::SEXP, Self::Error> {
709 self.try_into_sexp()
710 }
711
712 fn into_sexp(self) -> crate::SEXP {
713 unsafe {
716 let scope = crate::gc_protect::ProtectScope::new();
717 let pairs: Vec<(K, crate::SEXP)> = self
718 .0
719 .into_iter()
720 .map(|(k, v)| (k, scope.protect_raw(v.into_sexp())))
721 .collect();
722 List::from_raw_pairs(pairs).into_sexp()
723 }
724 }
725}
726
727impl<K: AsRef<str>, V: Clone + IntoR> IntoR for AsNamedList<&[(K, V)]> {
728 type Error = std::convert::Infallible;
729
730 fn try_into_sexp(self) -> Result<crate::SEXP, Self::Error> {
731 Ok(self.into_sexp())
732 }
733
734 unsafe fn try_into_sexp_unchecked(self) -> Result<crate::SEXP, Self::Error> {
735 self.try_into_sexp()
736 }
737
738 fn into_sexp(self) -> crate::SEXP {
739 unsafe {
742 let scope = crate::gc_protect::ProtectScope::new();
743 let pairs: Vec<(&K, crate::SEXP)> = self
744 .0
745 .iter()
746 .map(|(k, v)| (k, scope.protect_raw(v.clone().into_sexp())))
747 .collect();
748 List::from_raw_pairs(pairs).into_sexp()
749 }
750 }
751}
752
753#[derive(#[automatically_derived]
impl<T: ::core::fmt::Debug> ::core::fmt::Debug for AsNamedVector<T> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_tuple_field1_finish(f, "AsNamedVector",
&&self.0)
}
}Debug, #[automatically_derived]
impl<T: ::core::clone::Clone> ::core::clone::Clone for AsNamedVector<T> {
#[inline]
fn clone(&self) -> Self {
AsNamedVector(::core::clone::Clone::clone(&self.0))
}
}Clone)]
777pub struct AsNamedVector<T>(pub T);
778
779impl<T> From<T> for AsNamedVector<T> {
780 fn from(value: T) -> Self {
781 AsNamedVector(value)
782 }
783}
784
785impl<K: AsRef<str>, V: AtomicElement> IntoR for AsNamedVector<Vec<(K, V)>> {
786 type Error = std::convert::Infallible;
787
788 fn try_into_sexp(self) -> Result<crate::SEXP, Self::Error> {
789 Ok(self.into_sexp())
790 }
791
792 unsafe fn try_into_sexp_unchecked(self) -> Result<crate::SEXP, Self::Error> {
793 self.try_into_sexp()
794 }
795
796 fn into_sexp(self) -> crate::SEXP {
797 named_vector_from_pairs(self.0)
798 }
799}
800
801impl<K: AsRef<str>, V: AtomicElement, const N: usize> IntoR for AsNamedVector<[(K, V); N]> {
802 type Error = std::convert::Infallible;
803
804 fn try_into_sexp(self) -> Result<crate::SEXP, Self::Error> {
805 Ok(self.into_sexp())
806 }
807
808 unsafe fn try_into_sexp_unchecked(self) -> Result<crate::SEXP, Self::Error> {
809 self.try_into_sexp()
810 }
811
812 fn into_sexp(self) -> crate::SEXP {
813 named_vector_from_pairs(self.0)
814 }
815}
816
817impl<K: AsRef<str>, V: Clone + AtomicElement> IntoR for AsNamedVector<&[(K, V)]> {
818 type Error = std::convert::Infallible;
819
820 fn try_into_sexp(self) -> Result<crate::SEXP, Self::Error> {
821 Ok(self.into_sexp())
822 }
823
824 unsafe fn try_into_sexp_unchecked(self) -> Result<crate::SEXP, Self::Error> {
825 self.try_into_sexp()
826 }
827
828 fn into_sexp(self) -> crate::SEXP {
829 let (keys, values): (Vec<&K>, Vec<V>) = self.0.iter().map(|(k, v)| (k, v.clone())).unzip();
830 let sexp = V::vec_to_sexp(values);
831 unsafe {
832 let _guard = crate::OwnedProtect::new(sexp);
833 crate::named_vector::set_names_on_sexp(sexp, &keys);
834 }
835 sexp
836 }
837}
838
839fn named_vector_from_pairs<K, V>(pairs: impl IntoIterator<Item = (K, V)>) -> crate::SEXP
841where
842 K: AsRef<str>,
843 V: AtomicElement,
844{
845 let (keys, values): (Vec<K>, Vec<V>) = pairs.into_iter().unzip();
846 let sexp = V::vec_to_sexp(values);
847 unsafe {
848 let _guard = crate::OwnedProtect::new(sexp);
849 crate::named_vector::set_names_on_sexp(sexp, &keys);
850 }
851 sexp
852}
853pub trait AsListExt: IntoList + Sized {
884 fn wrap_list(self) -> AsList<Self> {
886 AsList(self)
887 }
888}
889
890impl<T: IntoList> AsListExt for T {}
891
892pub trait AsExternalPtrExt: IntoExternalPtr + Sized {
908 fn wrap_external_ptr(self) -> AsExternalPtr<Self> {
910 AsExternalPtr(self)
911 }
912}
913
914impl<T: IntoExternalPtr> AsExternalPtrExt for T {}
915
916pub trait AsRNativeExt: RNativeType + Sized {
929 fn wrap_r_native(self) -> AsRNative<Self> {
931 AsRNative(self)
932 }
933}
934
935impl<T: RNativeType> AsRNativeExt for T {}
936
937pub trait AsDataFrameExt: crate::dataframe::IntoDataFrame + Sized {
942 fn wrap_data_frame(self) -> AsDataFrame<Self> {
944 AsDataFrame(self)
945 }
946}
947
948impl<T: crate::dataframe::IntoDataFrame> AsDataFrameExt for T {}
949
950#[cfg(feature = "vctrs")]
954pub trait AsVctrsExt: crate::vctrs::IntoVctrs + Sized {
955 fn wrap_vctrs(self) -> AsVctrs<Self> {
957 AsVctrs(self)
958 }
959}
960
961#[cfg(feature = "vctrs")]
962impl<T: crate::vctrs::IntoVctrs> AsVctrsExt for T {}
963
964pub trait AsNamedListExt: Sized {
973 fn wrap_named_list(self) -> AsNamedList<Self> {
975 AsNamedList(self)
976 }
977}
978
979impl<K: AsRef<str>, V: IntoR> AsNamedListExt for Vec<(K, V)> {}
980impl<K: AsRef<str>, V: IntoR, const N: usize> AsNamedListExt for [(K, V); N] {}
981impl<K: AsRef<str>, V: Clone + IntoR> AsNamedListExt for &[(K, V)] {}
982
983pub trait AsNamedVectorExt: Sized {
992 fn wrap_named_vector(self) -> AsNamedVector<Self> {
994 AsNamedVector(self)
995 }
996}
997
998impl<K: AsRef<str>, V: AtomicElement> AsNamedVectorExt for Vec<(K, V)> {}
999impl<K: AsRef<str>, V: AtomicElement, const N: usize> AsNamedVectorExt for [(K, V); N] {}
1000impl<K: AsRef<str>, V: Clone + AtomicElement> AsNamedVectorExt for &[(K, V)] {}
1001#[derive(#[automatically_derived]
impl<T: ::core::fmt::Debug> ::core::fmt::Debug for AsDisplay<T> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_tuple_field1_finish(f, "AsDisplay",
&&self.0)
}
}Debug, #[automatically_derived]
impl<T: ::core::clone::Clone> ::core::clone::Clone for AsDisplay<T> {
#[inline]
fn clone(&self) -> Self {
AsDisplay(::core::clone::Clone::clone(&self.0))
}
}Clone, #[automatically_derived]
impl<T: ::core::marker::Copy> ::core::marker::Copy for AsDisplay<T> { }Copy)]
1022pub struct AsDisplay<T>(pub T);
1023
1024impl<T: std::fmt::Display> IntoR for AsDisplay<T> {
1025 type Error = std::convert::Infallible;
1026
1027 #[inline]
1028 fn try_into_sexp(self) -> Result<crate::SEXP, Self::Error> {
1029 Ok(self.0.to_string().into_sexp())
1030 }
1031
1032 #[inline]
1033 unsafe fn try_into_sexp_unchecked(self) -> Result<crate::SEXP, Self::Error> {
1034 Ok(unsafe { self.0.to_string().into_sexp_unchecked() })
1035 }
1036}
1037
1038#[derive(#[automatically_derived]
impl<T: ::core::fmt::Debug> ::core::fmt::Debug for AsDisplayVec<T> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_tuple_field1_finish(f, "AsDisplayVec",
&&self.0)
}
}Debug, #[automatically_derived]
impl<T: ::core::clone::Clone> ::core::clone::Clone for AsDisplayVec<T> {
#[inline]
fn clone(&self) -> Self {
AsDisplayVec(::core::clone::Clone::clone(&self.0))
}
}Clone)]
1049pub struct AsDisplayVec<T>(pub Vec<T>);
1050
1051impl<T: std::fmt::Display> IntoR for AsDisplayVec<T> {
1052 type Error = std::convert::Infallible;
1053
1054 #[inline]
1055 fn try_into_sexp(self) -> Result<crate::SEXP, Self::Error> {
1056 let strings: Vec<String> = self.0.into_iter().map(|x| x.to_string()).collect();
1057 Ok(strings.into_sexp())
1058 }
1059
1060 #[inline]
1061 unsafe fn try_into_sexp_unchecked(self) -> Result<crate::SEXP, Self::Error> {
1062 let strings: Vec<String> = self.0.into_iter().map(|x| x.to_string()).collect();
1063 Ok(unsafe { strings.into_sexp_unchecked() })
1064 }
1065}
1066
1067#[derive(#[automatically_derived]
impl<T: ::core::fmt::Debug> ::core::fmt::Debug for AsFromStr<T> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_tuple_field1_finish(f, "AsFromStr",
&&self.0)
}
}Debug, #[automatically_derived]
impl<T: ::core::clone::Clone> ::core::clone::Clone for AsFromStr<T> {
#[inline]
fn clone(&self) -> Self {
AsFromStr(::core::clone::Clone::clone(&self.0))
}
}Clone)]
1089pub struct AsFromStr<T>(pub T);
1090
1091impl<T: std::str::FromStr> crate::from_r::TryFromSexp for AsFromStr<T>
1092where
1093 T::Err: std::fmt::Display,
1094{
1095 type Error = crate::from_r::SexpError;
1096 const CHARACTER_ONLY: bool = true;
1097
1098 fn try_from_sexp(sexp: crate::SEXP) -> Result<Self, Self::Error> {
1099 let charsxp = crate::from_r::scalar_charsxp(sexp)?;
1100 if charsxp == crate::SEXP::na_string() {
1101 return Err(crate::from_r::SexpNaError {
1102 sexp_type: crate::SEXPTYPE::STRSXP,
1103 }
1104 .into());
1105 }
1106 let s = unsafe { crate::from_r::charsxp_to_str(charsxp) };
1108 s.parse::<T>()
1109 .map(AsFromStr)
1110 .map_err(|e| crate::from_r::SexpError::InvalidValue(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0:?}: {1}", s, e))
})format!("{s:?}: {e}")))
1111 }
1112}
1113
1114impl<T: std::fmt::Display> IntoR for AsFromStr<T> {
1120 type Error = <String as IntoR>::Error;
1121
1122 #[inline]
1123 fn try_into_sexp(self) -> Result<crate::SEXP, Self::Error> {
1124 self.0.to_string().try_into_sexp()
1125 }
1126
1127 #[inline]
1128 unsafe fn try_into_sexp_unchecked(self) -> Result<crate::SEXP, Self::Error> {
1129 unsafe { self.0.to_string().try_into_sexp_unchecked() }
1130 }
1131
1132 #[inline]
1133 fn into_sexp(self) -> crate::SEXP {
1134 self.0.to_string().into_sexp()
1135 }
1136
1137 #[inline]
1138 unsafe fn into_sexp_unchecked(self) -> crate::SEXP {
1139 unsafe { self.0.to_string().into_sexp_unchecked() }
1140 }
1141}
1142
1143#[derive(#[automatically_derived]
impl<T: ::core::fmt::Debug> ::core::fmt::Debug for AsFromStrVec<T> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_tuple_field1_finish(f, "AsFromStrVec",
&&self.0)
}
}Debug, #[automatically_derived]
impl<T: ::core::clone::Clone> ::core::clone::Clone for AsFromStrVec<T> {
#[inline]
fn clone(&self) -> Self {
AsFromStrVec(::core::clone::Clone::clone(&self.0))
}
}Clone)]
1166pub struct AsFromStrVec<T>(pub Vec<T>);
1167
1168impl<T: std::str::FromStr> crate::from_r::TryFromSexp for AsFromStrVec<T>
1169where
1170 T::Err: std::fmt::Display,
1171{
1172 type Error = crate::from_r::SexpError;
1173 const CHARACTER_ONLY: bool = true;
1174
1175 fn try_from_sexp(sexp: crate::SEXP) -> Result<Self, Self::Error> {
1176 let strings: Vec<Option<&str>> = crate::from_r::TryFromSexp::try_from_sexp(sexp)?;
1177 let mut result = Vec::with_capacity(strings.len());
1178 let mut errors = crate::from_r::BatchedErrors::default();
1179 for (i, s) in strings.into_iter().enumerate() {
1180 match s {
1181 None => errors.push(i, || "NA is not allowed".to_string()),
1182 Some(s) => match s.parse::<T>() {
1183 Ok(v) => result.push(v),
1184 Err(e) => errors.push(i, || ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0:?}: {1}", s, e))
})format!("{s:?}: {e}")),
1185 },
1186 }
1187 }
1188 if errors.is_empty() {
1189 Ok(AsFromStrVec(result))
1190 } else {
1191 Err(errors.into_element_error())
1192 }
1193 }
1194}
1195
1196impl<T: std::fmt::Display> IntoR for AsFromStrVec<T> {
1199 type Error = <Vec<String> as IntoR>::Error;
1200
1201 #[inline]
1202 fn try_into_sexp(self) -> Result<crate::SEXP, Self::Error> {
1203 display_strings(self.0).try_into_sexp()
1204 }
1205
1206 #[inline]
1207 unsafe fn try_into_sexp_unchecked(self) -> Result<crate::SEXP, Self::Error> {
1208 unsafe { display_strings(self.0).try_into_sexp_unchecked() }
1209 }
1210
1211 #[inline]
1212 fn into_sexp(self) -> crate::SEXP {
1213 display_strings(self.0).into_sexp()
1214 }
1215
1216 #[inline]
1217 unsafe fn into_sexp_unchecked(self) -> crate::SEXP {
1218 unsafe { display_strings(self.0).into_sexp_unchecked() }
1219 }
1220}
1221
1222fn display_strings<T: std::fmt::Display>(values: Vec<T>) -> Vec<String> {
1223 values.into_iter().map(|v| v.to_string()).collect()
1224}
1225pub struct Collect<I>(pub I);
1262
1263impl<I, T> IntoR for Collect<I>
1264where
1265 I: ExactSizeIterator<Item = T>,
1266 T: crate::RNativeType,
1267{
1268 type Error = std::convert::Infallible;
1269
1270 #[inline]
1271 fn try_into_sexp(self) -> Result<crate::SEXP, Self::Error> {
1272 Ok(self.into_sexp())
1273 }
1274
1275 #[inline]
1276 unsafe fn try_into_sexp_unchecked(self) -> Result<crate::SEXP, Self::Error> {
1277 Ok(unsafe { self.into_sexp_unchecked() })
1278 }
1279
1280 #[inline]
1281 fn into_sexp(self) -> crate::SEXP {
1282 unsafe {
1283 let (sexp, dst) = crate::into_r::alloc_r_vector::<T>(self.0.len());
1284 for (slot, val) in dst.iter_mut().zip(self.0) {
1285 *slot = val;
1286 }
1287 sexp
1288 }
1289 }
1290
1291 #[inline]
1292 unsafe fn into_sexp_unchecked(self) -> crate::SEXP {
1293 unsafe {
1294 let (sexp, dst) = crate::into_r::alloc_r_vector_unchecked::<T>(self.0.len());
1295 for (slot, val) in dst.iter_mut().zip(self.0) {
1296 *slot = val;
1297 }
1298 sexp
1299 }
1300 }
1301}
1302
1303pub struct CollectStrings<I>(pub I);
1318
1319impl<I> IntoR for CollectStrings<I>
1320where
1321 I: ExactSizeIterator<Item = String>,
1322{
1323 type Error = std::convert::Infallible;
1324
1325 #[inline]
1326 fn try_into_sexp(self) -> Result<crate::SEXP, Self::Error> {
1327 let strings: Vec<String> = self.0.collect();
1329 Ok(crate::into_r::str_iter_to_strsxp(
1330 strings.iter().map(|s| s.as_str()),
1331 ))
1332 }
1333
1334 #[inline]
1335 unsafe fn try_into_sexp_unchecked(self) -> Result<crate::SEXP, Self::Error> {
1336 let strings: Vec<String> = self.0.collect();
1337 Ok(unsafe {
1338 crate::into_r::str_iter_to_strsxp_unchecked(strings.iter().map(|s| s.as_str()))
1339 })
1340 }
1341}
1342
1343pub struct CollectNA<I>(pub I);
1359
1360impl<I> IntoR for CollectNA<I>
1361where
1362 I: ExactSizeIterator<Item = Option<f64>>,
1363{
1364 type Error = std::convert::Infallible;
1365
1366 #[inline]
1367 fn try_into_sexp(self) -> Result<crate::SEXP, Self::Error> {
1368 Ok(self.into_sexp())
1369 }
1370
1371 #[inline]
1372 unsafe fn try_into_sexp_unchecked(self) -> Result<crate::SEXP, Self::Error> {
1373 Ok(unsafe { self.into_sexp_unchecked() })
1374 }
1375
1376 #[inline]
1377 fn into_sexp(self) -> crate::SEXP {
1378 unsafe {
1379 let (sexp, dst) = crate::into_r::alloc_r_vector::<f64>(self.0.len());
1380 for (slot, val) in dst.iter_mut().zip(self.0) {
1381 *slot = val.unwrap_or(crate::altrep_traits::NA_REAL);
1382 }
1383 sexp
1384 }
1385 }
1386
1387 #[inline]
1388 unsafe fn into_sexp_unchecked(self) -> crate::SEXP {
1389 unsafe {
1390 let (sexp, dst) = crate::into_r::alloc_r_vector_unchecked::<f64>(self.0.len());
1391 for (slot, val) in dst.iter_mut().zip(self.0) {
1392 *slot = val.unwrap_or(crate::altrep_traits::NA_REAL);
1393 }
1394 sexp
1395 }
1396 }
1397}
1398
1399pub struct CollectNAInt<I>(pub I);
1404
1405impl<I> IntoR for CollectNAInt<I>
1406where
1407 I: ExactSizeIterator<Item = Option<i32>>,
1408{
1409 type Error = std::convert::Infallible;
1410
1411 #[inline]
1412 fn try_into_sexp(self) -> Result<crate::SEXP, Self::Error> {
1413 Ok(self.into_sexp())
1414 }
1415
1416 #[inline]
1417 unsafe fn try_into_sexp_unchecked(self) -> Result<crate::SEXP, Self::Error> {
1418 Ok(unsafe { self.into_sexp_unchecked() })
1419 }
1420
1421 #[inline]
1422 fn into_sexp(self) -> crate::SEXP {
1423 unsafe {
1424 let (sexp, dst) = crate::into_r::alloc_r_vector::<i32>(self.0.len());
1425 for (slot, val) in dst.iter_mut().zip(self.0) {
1426 *slot = val.unwrap_or(crate::altrep_traits::NA_INTEGER);
1427 }
1428 sexp
1429 }
1430 }
1431
1432 #[inline]
1433 unsafe fn into_sexp_unchecked(self) -> crate::SEXP {
1434 unsafe {
1435 let (sexp, dst) = crate::into_r::alloc_r_vector_unchecked::<i32>(self.0.len());
1436 for (slot, val) in dst.iter_mut().zip(self.0) {
1437 *slot = val.unwrap_or(crate::altrep_traits::NA_INTEGER);
1438 }
1439 sexp
1440 }
1441 }
1442}
1443#[cfg(test)]
1446mod tests {
1447 use super::*;
1448
1449 #[test]
1455 fn reading_markers_are_into_r() {
1456 fn into_r<T: IntoR>() {}
1457 into_r::<AsNumeric>();
1458 into_r::<AsNumericVec>();
1459 into_r::<Option<AsNumeric>>();
1460 into_r::<Option<AsNumericVec>>();
1461 into_r::<AsCharacter>();
1462 into_r::<AsCharacterVec>();
1463 into_r::<Option<AsCharacter>>();
1464 into_r::<Option<AsCharacterVec>>();
1465 into_r::<AsFromStr<std::net::IpAddr>>();
1466 into_r::<AsFromStrVec<std::net::IpAddr>>();
1467 }
1468}