Skip to content

Commit 5e5dbd9

Browse files
Implement FromStr for JsString and JsValue
1 parent 872c57e commit 5e5dbd9

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

crates/js-sys/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818
1919
#![doc(html_root_url = "https://docs.rs/js-sys/0.2")]
2020

21+
use std::convert;
2122
use std::f64;
2223
use std::fmt;
2324
use std::mem;
25+
use std::str;
2426

2527
use wasm_bindgen::prelude::*;
2628
use wasm_bindgen::JsCast;
@@ -4297,6 +4299,13 @@ impl fmt::Debug for JsString {
42974299
}
42984300
}
42994301

4302+
impl str::FromStr for JsString {
4303+
type Err = convert::Infallible;
4304+
fn from_str(s: &str) -> Result<Self, Self::Err> {
4305+
Ok(JsString::from(s))
4306+
}
4307+
}
4308+
43004309
// Symbol
43014310
#[wasm_bindgen]
43024311
extern "C" {

src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
#![doc(html_root_url = "https://docs.rs/wasm-bindgen/0.2")]
1111
#![cfg_attr(feature = "nightly", feature(unsize))]
1212

13+
use core::convert::Infallible;
1314
use core::fmt;
1415
use core::marker;
1516
use core::mem;
1617
use core::ops::{Deref, DerefMut};
18+
use core::str::FromStr;
1719

1820
use crate::convert::{FromWasmAbi, WasmOptionalF64, WasmSlice};
1921

@@ -395,6 +397,13 @@ impl<'a> From<&'a str> for JsValue {
395397
}
396398
}
397399

400+
impl FromStr for JsValue {
401+
type Err = Infallible;
402+
fn from_str(s: &str) -> Result<Self, Self::Err> {
403+
Ok(Self::from_str(s))
404+
}
405+
}
406+
398407
if_std! {
399408
impl<'a> From<&'a String> for JsValue {
400409
#[inline]

0 commit comments

Comments
 (0)