Skip to content

Instantly share code, notes, and snippets.

@ivanpierre
Last active March 22, 2021 20:08
Show Gist options
  • Save ivanpierre/ace7138676c3a261e6460c0f58cf29fb to your computer and use it in GitHub Desktop.
Save ivanpierre/ace7138676c3a261e6460c0f58cf29fb to your computer and use it in GitHub Desktop.
At last some light in the making of a dynamic version of Rust with reified Structs and Protocols w/o garbage collector and w/o unsafe code (for now.)
#[test]
fn bidirectionnal_convert() {
// Test object with primitives
use std::any::TypeId;
use crate::clojure;
use clojure::rust::nil::*;
use clojure::rust::number::*;
use clojure::rust::object::*;
let nil: Object = Nil::new();
let number: Object = BigInteger::new(1);
println!("count at begining={:?}", number.count());
let number2: Object = number.clone();
println!("count after clone={:?}", number.count());
println!("original Object: {:?}", &number);
println!("cloned Object: {:?}", &number2);
// cast Object BigInteger -> Protocol Number
if let Some(number_as_number) = number.cast::<&dyn Number>() {
print!("number_as_number = {}", number_as_number.double_value());
}
// cast Object Nil -> Protocol Number
if let Some(nil_as_number) = nil.cast::<&dyn Number>() {
print!(
"nil_as_number = {} is a number",
nil_as_number.double_value()
);
} else {
print!("nil_as_number = {} is not a number", nil.to_string());
}
// Cast Object Nil -> unboxed Nil struct
if let Some(nil_as_struct_nil) = nil.cast::<Nil>() {
print!("Unboxed struct of Nil Object: {:?}", nil_as_struct_nil);
}
let type_id: TypeId = number.inner.type_id();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment