Function artichoke_backend::convert::convert_type

source ·
pub fn convert_type(
    interp: &mut Artichoke,
    value: Value,
    convert_to: Ruby,
    type_name: &str,
    method: &str,
    raise: ConvertOnError
) -> Result<Value, Error>
Expand description

Attempt a fallible conversion of a Ruby value to a given type tag.

This function can convert Ruby values at the granularity of a Ruby type tag. Conversion works as follows:

  • If the given value has the same type tag as the given convert_to, return the given value.
  • Assert that the given conversion method is a valid conversion type.
  • Call the conversion method on the given value. If this method raises, return the error.
  • If the converted value does not match the given type tag, raise a TypeError.
  • The converted value matches the target type, return it.

§Conversion types

The method to be called to perform the implicit conversion must be one of a permitted set. Valid method calls are:

  • to_int
  • to_ary
  • to_str
  • to_sym
  • to_hash
  • to_proc
  • to_io
  • to_a
  • to_s
  • to_i
  • to_f
  • to_r

§MRI Compatibility

This function is modeled after the rb_convert_type C API in MRI Ruby.

§Panics

If the given method is not a valid conversion method, this function will panic.

§Errors

  • If the call to the conversion method returns an error, that error is returned.
  • If the call to the conversion method returns a value that does not match the target type tag, a TypeError is returned.