-
-
Notifications
You must be signed in to change notification settings - Fork 217
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor GodotString
, NodePath
, and StringName
#233
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
#![macro_use] | ||
|
||
macro_rules! impl_rust_string_conv { | ||
($Ty:ty) => { | ||
impl<S> From<S> for $Ty | ||
where | ||
S: AsRef<str>, | ||
{ | ||
fn from(string: S) -> Self { | ||
let intermediate = GodotString::from(string.as_ref()); | ||
Self::from(&intermediate) | ||
} | ||
} | ||
Comment on lines
+11
to
+19
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the case of GodotString::from(&str) and then GodotString::from(&GodotString) Which trait impl powers the 2nd conversion? There is no The background behind my question is that it might perform an unnecessary conversion/copy. fn from(string: S) -> Self {
// note: no &
Self::from(GodotString::from(string.as_ref()));
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't use this macro for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I see. In that case you can go ahead! 🙂 |
||
|
||
impl From<&$Ty> for String { | ||
fn from(string: &$Ty) -> Self { | ||
let intermediate = GodotString::from(string); | ||
Self::from(&intermediate) | ||
} | ||
} | ||
|
||
impl From<$Ty> for String { | ||
fn from(string: $Ty) -> Self { | ||
Self::from(&string) | ||
} | ||
} | ||
|
||
impl std::str::FromStr for $Ty { | ||
type Err = std::convert::Infallible; | ||
|
||
fn from_str(string: &str) -> Result<Self, Self::Err> { | ||
Ok(Self::from(string)) | ||
} | ||
} | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
//! Godot-types that are Strings. | ||
mod godot_string; | ||
mod macros; | ||
mod node_path; | ||
mod string_chars; | ||
mod string_name; | ||
|
||
use godot_ffi::VariantType; | ||
pub use godot_string::*; | ||
pub use node_path::*; | ||
pub use string_name::*; | ||
|
||
use super::{meta::VariantMetadata, FromVariant, ToVariant, Variant, VariantConversionError}; | ||
|
||
impl ToVariant for &str { | ||
fn to_variant(&self) -> Variant { | ||
GodotString::from(*self).to_variant() | ||
} | ||
} | ||
|
||
impl ToVariant for String { | ||
fn to_variant(&self) -> Variant { | ||
GodotString::from(self).to_variant() | ||
} | ||
} | ||
|
||
impl FromVariant for String { | ||
fn try_from_variant(variant: &Variant) -> Result<Self, VariantConversionError> { | ||
Ok(GodotString::try_from_variant(variant)?.to_string()) | ||
} | ||
} | ||
|
||
impl VariantMetadata for String { | ||
fn variant_type() -> VariantType { | ||
VariantType::String | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This name may shadow the
Hash::hash(&mut H)
method from the standard library. Should probably be fine though, as the trait can be invoked with fully-qualified syntax.