/// Safely casts current object to the specified Ruby type
///
/// This function is used by `methods!` macro for argument casting.
///
/// See documentation for `VerifiedObject` trait to enable safe conversions
/// for custom classes.
///
/// # Examples
///
/// ### Basic conversions
///
/// ```
/// use rutie::{AnyException, Exception, Fixnum, Object, RString, VM};
/// # VM::init();
///
/// let fixnum_as_any_object = Fixnum::new(1).to_any_object();
/// let converted_fixnum = fixnum_as_any_object.try_convert_to::<Fixnum>();
///
/// assert_eq!(converted_fixnum, Ok(Fixnum::new(1)));
///
/// let string = RString::new_utf8("string");
/// let string_as_fixnum = string.try_convert_to::<Fixnum>();
/// let expected_error = AnyException::new("TypeError", Some("Error converting to Fixnum"));
///
/// assert_eq!(string_as_fixnum, Err(expected_error));
/// ```
///
/// ### Method arguments
///
/// To launch a server in Rust, you plan to write a simple `Server` class
///
/// ```ruby
/// class Server
///   def start(address)
///     # ...
///   end
/// end
/// ```
///
/// The `address` must be `Hash` with the following structure:
///
/// ```ruby
/// {
///   host: 'localhost',
///   port: 8080,
/// }
/// ```
///
/// You want to extract port from it. Default port is `8080` in case when:
///
///  - `address` is not a `Hash`
///  - `address[:port]` is not present
///  - `address[:port]` is not a `Fixnum`
///
/// ```no_run
/// #[macro_use]
/// extern crate rutie;
///
/// use rutie::{Class, Fixnum, Hash, NilClass, Object, Symbol, VM};
///
/// class!(Server);
///
/// methods!(
///     Server,
///     rtself,
///
///     fn start(address: Hash) -> NilClass {
///         let default_port = 8080;
///
///         let port = address
///             .map(|hash| hash.at(&Symbol::new("port")))
///             .and_then(|port| port.try_convert_to::<Fixnum>())
///             .map(|port| port.to_i64())
///             .unwrap_or(default_port);
///
///         // Start server...
///
///         NilClass::new()
///     }
/// );
///
/// fn main() {
///     # VM::init();
///     Class::new("Server", None).define(|klass| {
///         klass.def("start", start);
///     });
/// }
/// ```
///
/// Ruby:
///
/// ```ruby
/// class Server
///   def start(address)
///     default_port = 8080
///
///     port =
///       if address.is_a?(Hash) && address[:port].is_a?(Fixnum)
///         address[:port]
///       else
///         default_port
///       end
///
///     # Start server...
///   end
/// end
/// ```
Safely casts current object to the specified Ruby type
This function is used by `methods!` macro for argument casting.

See documentation for `VerifiedObject` trait to enable safe conversions
for custom classes.
Safely casts current object to the specified Ruby type This function is used by ` methods! ` macro for argument casting . See documentation for ` VerifiedObject ` trait to enable safe conversions for custom classes .
fn try_convert_to<T: VerifiedObject>(&self) -> Result<T, AnyException> {
        if T::is_correct_type(self) {
            let converted_object = unsafe { self.to::<T>() };
            Ok(converted_object)
        } else {
            Err(AnyException::new("TypeError", Some(T::error_message())))
        }
    }
fn try_convert_to < T : VerifiedObject > ( & self ) -> Result < T , AnyException > { if T :: is_correct_type ( self ) { let converted_object = unsafe { self . to :: < T > ( ) } ; Ok ( converted_object ) } else { Err ( AnyException :: new ( " " , Some ( T :: error_message ( ) ) ) ) } }
Safely casts current object to the specified Ruby type
This function is used by `methods!` macro for argument casting.
Safely casts current object to the specified Ruby type This function is used by ` methods! ` macro for argument casting .
&self
examples
None
basic conversions
```
use rutie::{AnyException, Exception, Fixnum, Object, RString, VM}.
` ` ` use rutie :: { AnyException Exception Fixnum Object RString VM } .
vm::init();
let fixnum_as_any_object = Fixnum::new(1).to_any_object();
let converted_fixnum = fixnum_as_any_object.try_convert_to::().

assert_eq!(converted_fixnum, Ok(Fixnum::new(1))).

let string = RString::new_utf8("string");
let string_as_fixnum = string.try_convert_to::();
let expected_error = AnyException::new("TypeError", Some("Error converting to Fixnum")).

assert_eq!(string_as_fixnum, Err(expected_error));
```
let fixnum_as_any_object = Fixnum :: new ( 1 ) . to_any_object () ; let converted_fixnum = fixnum_as_any_object . try_convert_to :: () . assert_eq! ( converted_fixnum Ok ( Fixnum :: new ( 1 ))) . let string = RString :: new_utf8 ( " string " ) ; let string_as_fixnum = string . try_convert_to :: () ; let expected_error = AnyException :: new ( " TypeError " Some ( " Error converting to Fixnum " )) . assert_eq! ( string_as_fixnum Err ( expected_error )) ; ` ` `
method arguments
To launch a server in Rust, you plan to write a simple `Server` class

```ruby
class Server
def start(address)

end
end
```

The `address` must be `Hash` with the following structure.



You want to extract port from it. Default port is `8080` in case when.

`address` is not a `Hash`
`address[:port]` is not present
`address[:port]` is not a `Fixnum`


To launch a server in Rust you plan to write a simple ` Server ` class ` ` ` ruby class Server def start ( address ) end end ` ` ` The ` address ` must be ` Hash ` with the following structure . You want to extract port from it . Default port is ` 8080 ` in case when . ` address ` is not a ` Hash ` ` address [ : port ] ` is not present ` address [ : port ] ` is not a ` Fixnum `
[macro_use]
extern crate rutie.

use rutie::{Class, Fixnum, Hash, NilClass, Object, Symbol, VM}.

class!(Server).

methods!(
Server,
rtself.

fn start(address: Hash) -> NilClass {
let default_port = 8080.



Start server







```ruby
class Server
def start(address)
default_port = 8080

port
if address.is_a?(Hash) && address[:port].is_a?(Fixnum)
address[:port]
else
default_port
end

Start server
end
end
```
extern crate rutie . use rutie :: { Class Fixnum Hash NilClass Object Symbol VM } . class! ( Server ) . methods! ( Server rtself . fn start ( address : Hash ) - > NilClass { let default_port = 8080 . Start server ` ` ` ruby class Server def start ( address ) default_port = 8080 port if address . is_a? ( Hash ) && address [ : port ] . is_a? ( Fixnum ) address [ : port ] else default_port end Start server end end ` ` `