pub trait ToJsValue {
// Required method
fn to_js_value(&self) -> String;
}Expand description
A trait for converting Rust types to JavaScript value representations.
Types that implement this trait can be used in js! macro interpolation
with the #{expr} syntax.
§Built-in Implementations
- Integers (
i8,i16,i32,i64,i128,isize,u8,u16,u32,u64,u128,usize): Converted to number strings - Floats (
f32,f64): Converted to number strings, with special handling forInfinity,-Infinity, andNaN - Booleans: Converted to
"true"or"false" - Strings (
str,String): Properly quoted and escaped Option<T>:Some(v)delegates tov.to_js_value(),Nonebecomes"null"- References: Delegates to the inner type
§Examples
use viewpoint_js_core::ToJsValue;
assert_eq!(42.to_js_value(), "42");
assert_eq!(true.to_js_value(), "true");
assert_eq!("hello".to_js_value(), r#""hello""#);
assert_eq!(Some(42).to_js_value(), "42");
assert_eq!(None::<i32>.to_js_value(), "null");Required Methods§
Sourcefn to_js_value(&self) -> String
fn to_js_value(&self) -> String
Convert this value to a JavaScript representation.
The returned string should be valid JavaScript that represents this value. For example:
- Integers:
"42" - Strings:
"\"hello\"" - Booleans:
"true"or"false" - null:
"null"