Skip to content

Commit afa2e59

Browse files
bors[bot]juliohq
andauthored
Merge #201
201: Implement FromStr for NodePath r=Bromeon a=juliohq Implements `FromStr` for `NodePath`. Co-authored-by: juliohq <[email protected]>
2 parents eb30a8b + 0556d52 commit afa2e59

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

godot-core/src/builtin/node_path.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
55
*/
66

7+
use std::convert::Infallible;
78
use std::fmt;
9+
use std::str::FromStr;
810

911
use crate::builtin::GodotString;
1012
use godot_ffi as sys;
@@ -60,6 +62,14 @@ impl From<&str> for NodePath {
6062
}
6163
}
6264

65+
impl FromStr for NodePath {
66+
type Err = Infallible;
67+
68+
fn from_str(path: &str) -> Result<Self, Self::Err> {
69+
Ok(Self::from(path))
70+
}
71+
}
72+
6373
impl fmt::Display for NodePath {
6474
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
6575
let string = GodotString::from(self);

itest/rust/src/node_test.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ use godot::builtin::{NodePath, Variant};
99
use godot::engine::{global, node, Node, Node3D, NodeExt, PackedScene, SceneTree};
1010
use godot::obj::Share;
1111

12+
use std::str::FromStr;
13+
1214
#[itest]
1315
fn node_get_node() {
1416
let mut child = Node3D::new_alloc();
@@ -54,6 +56,15 @@ fn node_get_node_fail() {
5456
child.free();
5557
}
5658

59+
#[itest]
60+
fn node_path_from_str(ctx: &TestContext) {
61+
let child = ctx.scene_tree.share();
62+
assert_eq!(
63+
child.get_path().to_string(),
64+
NodePath::from_str("/root/TestRunner").unwrap().to_string()
65+
);
66+
}
67+
5768
#[itest(skip)]
5869
fn node_scene_tree() {
5970
let mut child = Node::new_alloc();

0 commit comments

Comments
 (0)