Skip to main content

nanvm_lib/vm/string/
from.rs

1use crate::vm::{Any, IVm, String, ToAny, ToString};
2
3impl<A: IVm> From<&str> for String<A> {
4    fn from(value: &str) -> Self {
5        value.encode_utf16().to_string()
6    }
7}
8
9impl<A: IVm> From<&str> for Any<A> {
10    fn from(value: &str) -> Self {
11        let s: String<_> = value.into();
12        s.to_any()
13    }
14}
15
16/// Lossy: a lone surrogate, which a JavaScript string may hold and a Rust
17/// string cannot, becomes U+FFFD. Use `char::decode_utf16` directly where a
18/// lone surrogate must be told apart from a real U+FFFD.
19impl<A: IVm> From<String<A>> for std::string::String {
20    fn from(value: String<A>) -> Self {
21        char::decode_utf16(value)
22            .map(|r| r.unwrap_or(char::REPLACEMENT_CHARACTER))
23            .collect()
24    }
25}