Skip to content

Commit

Permalink
make try_convert module public so TryConvertOwned can be named
Browse files Browse the repository at this point in the history
  • Loading branch information
matsadler committed May 27, 2023
1 parent 8f5e30d commit f0ba8b8
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 69 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
### Added

### Changed
- `try_convert` module is now public so that `TryConvertOwned` can be
named/implemented.

### Deprecated

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1860,7 +1860,7 @@ pub mod rb_sys;
pub mod ruby_handle;
pub mod scan_args;
mod symbol;
mod try_convert;
pub mod try_convert;
pub mod typed_data;
pub mod value;

Expand Down
7 changes: 1 addition & 6 deletions src/r_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -802,12 +802,7 @@ impl RArray {
where
T: TryConvertOwned,
{
unsafe {
self.as_slice()
.iter()
.map(|v| T::try_convert_owned(*v))
.collect()
}
unsafe { self.as_slice().iter().map(|v| T::try_convert(*v)).collect() }
}

/// Convert `self` to a Rust array of [`Value`]s, of length `N`.
Expand Down
102 changes: 46 additions & 56 deletions src/try_convert.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Traits for converting from Ruby [`Value`]s to Rust types.
use std::path::PathBuf;

use rb_sys::{rb_get_path, rb_num2dbl};
Expand All @@ -20,20 +22,16 @@ pub trait TryConvert: Sized {
fn try_convert(val: Value) -> Result<Self, Error>;
}

/// Only implemented on Rust types. TryConvert may convert from a
/// Value to another Ruby type. Use this when you need a Rust value that's
/// divorced from the Ruby runtime, safe to put on the heap, etc.
pub trait TryConvertOwned: TryConvert {
/// Convert `val` into `Self`.
fn try_convert_owned(val: Value) -> Result<Self, Error> {
Self::try_convert(val)
}
}

pub trait TryConvertForArg: Sized {
/// Convert `val` into `Self`.
fn try_convert_for_arg(val: Value) -> Result<Self, Error>;
}
/// Conversions from [`Value`] to Rust types that do not contain [`Value`].
///
/// This trait is used as a bound on some implementations of [`TryConvert`]
/// (for example, for [`Vec`]) to prevent heap allocated datastructures
/// containing `Value`, as it is not safe to store a `Value` on the heap.
///
/// # Safety
///
/// This trait must not be implemented for types that contain `Value`.
pub unsafe trait TryConvertOwned: TryConvert {}

impl<T> TryConvert for Option<T>
where
Expand All @@ -44,100 +42,91 @@ where
}
}

impl<T> TryConvertOwned for Option<T>
where
T: TryConvertOwned,
{
fn try_convert_owned(val: Value) -> Result<Self, Error> {
(!val.is_nil())
.then(|| T::try_convert_owned(val))
.transpose()
}
}
unsafe impl<T> TryConvertOwned for Option<T> where T: TryConvertOwned {}

impl TryConvert for bool {
fn try_convert(val: Value) -> Result<Self, Error> {
Ok(val.to_bool())
}
}
impl TryConvertOwned for bool {}
unsafe impl TryConvertOwned for bool {}

impl TryConvert for i8 {
fn try_convert(val: Value) -> Result<Self, Error> {
Integer::try_convert(val)?.to_i8()
}
}
impl TryConvertOwned for i8 {}
unsafe impl TryConvertOwned for i8 {}

impl TryConvert for i16 {
fn try_convert(val: Value) -> Result<Self, Error> {
Integer::try_convert(val)?.to_i16()
}
}
impl TryConvertOwned for i16 {}
unsafe impl TryConvertOwned for i16 {}

impl TryConvert for i32 {
fn try_convert(val: Value) -> Result<Self, Error> {
Integer::try_convert(val)?.to_i32()
}
}
impl TryConvertOwned for i32 {}
unsafe impl TryConvertOwned for i32 {}

impl TryConvert for i64 {
fn try_convert(val: Value) -> Result<Self, Error> {
Integer::try_convert(val)?.to_i64()
}
}
impl TryConvertOwned for i64 {}
unsafe impl TryConvertOwned for i64 {}

impl TryConvert for isize {
fn try_convert(val: Value) -> Result<Self, Error> {
Integer::try_convert(val)?.to_isize()
}
}
impl TryConvertOwned for isize {}
unsafe impl TryConvertOwned for isize {}

impl TryConvert for u8 {
fn try_convert(val: Value) -> Result<Self, Error> {
Integer::try_convert(val)?.to_u8()
}
}
impl TryConvertOwned for u8 {}
unsafe impl TryConvertOwned for u8 {}

impl TryConvert for u16 {
fn try_convert(val: Value) -> Result<Self, Error> {
Integer::try_convert(val)?.to_u16()
}
}
impl TryConvertOwned for u16 {}
unsafe impl TryConvertOwned for u16 {}

impl TryConvert for u32 {
fn try_convert(val: Value) -> Result<Self, Error> {
Integer::try_convert(val)?.to_u32()
}
}
impl TryConvertOwned for u32 {}
unsafe impl TryConvertOwned for u32 {}

impl TryConvert for u64 {
fn try_convert(val: Value) -> Result<Self, Error> {
Integer::try_convert(val)?.to_u64()
}
}
impl TryConvertOwned for u64 {}
unsafe impl TryConvertOwned for u64 {}

impl TryConvert for usize {
fn try_convert(val: Value) -> Result<Self, Error> {
Integer::try_convert(val)?.to_usize()
}
}
impl TryConvertOwned for usize {}
unsafe impl TryConvertOwned for usize {}

impl TryConvert for f32 {
fn try_convert(val: Value) -> Result<Self, Error> {
f64::try_convert(val).map(|f| f as f32)
}
}
impl TryConvertOwned for f32 {}
unsafe impl TryConvertOwned for f32 {}

impl TryConvert for f64 {
fn try_convert(val: Value) -> Result<Self, Error> {
Expand All @@ -157,15 +146,15 @@ impl TryConvert for f64 {
Ok(res)
}
}
impl TryConvertOwned for f64 {}
unsafe impl TryConvertOwned for f64 {}

impl TryConvert for String {
fn try_convert(val: Value) -> Result<Self, Error> {
debug_assert_value!(val);
RString::try_convert(val)?.to_string()
}
}
impl TryConvertOwned for String {}
unsafe impl TryConvertOwned for String {}

#[cfg(feature = "bytes-crate")]
impl TryConvert for bytes::Bytes {
Expand All @@ -176,15 +165,15 @@ impl TryConvert for bytes::Bytes {
}

#[cfg(feature = "bytes-crate")]
impl TryConvertOwned for bytes::Bytes {}
unsafe impl TryConvertOwned for bytes::Bytes {}

impl TryConvert for char {
fn try_convert(val: Value) -> Result<Self, Error> {
debug_assert_value!(val);
RString::try_convert(val)?.to_char()
}
}
impl TryConvertOwned for char {}
unsafe impl TryConvertOwned for char {}

impl<T> TryConvert for Vec<T>
where
Expand All @@ -195,7 +184,7 @@ where
RArray::try_convert(val)?.to_vec()
}
}
impl<T> TryConvertOwned for Vec<T> where T: TryConvertOwned {}
unsafe impl<T> TryConvertOwned for Vec<T> where T: TryConvertOwned {}

impl<T, const N: usize> TryConvert for [T; N]
where
Expand All @@ -206,7 +195,7 @@ where
RArray::try_convert(val)?.to_array()
}
}
impl<T, const N: usize> TryConvertOwned for [T; N] where T: TryConvert {}
unsafe impl<T, const N: usize> TryConvertOwned for [T; N] where T: TryConvert {}

impl<T0> TryConvert for (T0,)
where
Expand All @@ -225,7 +214,7 @@ where
Ok((slice[0].try_convert()?,))
}
}
impl<T0> TryConvertOwned for (T0,) where T0: TryConvert {}
unsafe impl<T0> TryConvertOwned for (T0,) where T0: TryConvert {}

impl<T0, T1> TryConvert for (T0, T1)
where
Expand All @@ -245,7 +234,7 @@ where
Ok((slice[0].try_convert()?, slice[1].try_convert()?))
}
}
impl<T0, T1> TryConvertOwned for (T0, T1)
unsafe impl<T0, T1> TryConvertOwned for (T0, T1)
where
T0: TryConvert,
T1: TryConvert,
Expand Down Expand Up @@ -275,7 +264,7 @@ where
))
}
}
impl<T0, T1, T2> TryConvertOwned for (T0, T1, T2)
unsafe impl<T0, T1, T2> TryConvertOwned for (T0, T1, T2)
where
T0: TryConvert,
T1: TryConvert,
Expand Down Expand Up @@ -308,7 +297,7 @@ where
))
}
}
impl<T0, T1, T2, T3> TryConvertOwned for (T0, T1, T2, T3)
unsafe impl<T0, T1, T2, T3> TryConvertOwned for (T0, T1, T2, T3)
where
T0: TryConvert,
T1: TryConvert,
Expand Down Expand Up @@ -344,7 +333,7 @@ where
))
}
}
impl<T0, T1, T2, T3, T4> TryConvertOwned for (T0, T1, T2, T3, T4)
unsafe impl<T0, T1, T2, T3, T4> TryConvertOwned for (T0, T1, T2, T3, T4)
where
T0: TryConvert,
T1: TryConvert,
Expand Down Expand Up @@ -383,7 +372,7 @@ where
))
}
}
impl<T0, T1, T2, T3, T4, T5> TryConvertOwned for (T0, T1, T2, T3, T4, T5)
unsafe impl<T0, T1, T2, T3, T4, T5> TryConvertOwned for (T0, T1, T2, T3, T4, T5)
where
T0: TryConvert,
T1: TryConvert,
Expand Down Expand Up @@ -425,7 +414,7 @@ where
))
}
}
impl<T0, T1, T2, T3, T4, T5, T6> TryConvertOwned for (T0, T1, T2, T3, T4, T5, T6)
unsafe impl<T0, T1, T2, T3, T4, T5, T6> TryConvertOwned for (T0, T1, T2, T3, T4, T5, T6)
where
T0: TryConvert,
T1: TryConvert,
Expand Down Expand Up @@ -470,7 +459,7 @@ where
))
}
}
impl<T0, T1, T2, T3, T4, T5, T6, T7> TryConvertOwned for (T0, T1, T2, T3, T4, T5, T6, T7)
unsafe impl<T0, T1, T2, T3, T4, T5, T6, T7> TryConvertOwned for (T0, T1, T2, T3, T4, T5, T6, T7)
where
T0: TryConvert,
T1: TryConvert,
Expand Down Expand Up @@ -518,7 +507,8 @@ where
))
}
}
impl<T0, T1, T2, T3, T4, T5, T6, T7, T8> TryConvertOwned for (T0, T1, T2, T3, T4, T5, T6, T7, T8)
unsafe impl<T0, T1, T2, T3, T4, T5, T6, T7, T8> TryConvertOwned
for (T0, T1, T2, T3, T4, T5, T6, T7, T8)
where
T0: TryConvert,
T1: TryConvert,
Expand Down Expand Up @@ -569,7 +559,7 @@ where
))
}
}
impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> TryConvertOwned
unsafe impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> TryConvertOwned
for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9)
where
T0: TryConvert,
Expand Down Expand Up @@ -625,7 +615,7 @@ where
))
}
}
impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> TryConvertOwned
unsafe impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> TryConvertOwned
for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)
where
T0: TryConvert,
Expand Down Expand Up @@ -684,7 +674,7 @@ where
))
}
}
impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> TryConvertOwned
unsafe impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> TryConvertOwned
for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)
where
T0: TryConvert,
Expand Down Expand Up @@ -712,7 +702,7 @@ where
RHash::try_convert(val)?.to_hash_map()
}
}
impl<K, V> TryConvertOwned for std::collections::HashMap<K, V>
unsafe impl<K, V> TryConvertOwned for std::collections::HashMap<K, V>
where
K: TryConvertOwned + Eq + std::hash::Hash,
V: TryConvertOwned,
Expand Down Expand Up @@ -742,4 +732,4 @@ impl TryConvert for PathBuf {
}
}

impl TryConvertOwned for PathBuf {}
unsafe impl TryConvertOwned for PathBuf {}
Loading

0 comments on commit f0ba8b8

Please sign in to comment.