Skip to content

Implement GString concatenation operator #1117

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions godot-core/src/builtin/string/gstring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@
use std::convert::Infallible;
use std::fmt;
use std::fmt::Write;
use std::ops::Add;

use godot_ffi as sys;
use sys::types::OpaqueString;
use sys::{ffi_methods, interface_fn, GodotFfi};

use crate::builtin::string::Encoding;
use crate::builtin::{inner, NodePath, StringName, Variant};
use crate::global::str;
use crate::meta::error::StringError;
use crate::meta::AsArg;
use crate::meta::ToGodot;
use crate::{impl_shared_string_api, meta};

/// Godot's reference counted string type.
Expand Down Expand Up @@ -314,6 +317,33 @@ impl fmt::Debug for GString {
}
}

// ----------------------------------------------------------------------------------------------------------------------------------------------
// Concatentation
impl<'a, 'b> Add<&'b GString> for &'a GString {
type Output = GString;

fn add(self, other: &'b GString) -> GString {
str(&[self.to_variant(), other.to_variant()])
}
}

impl<'a> Add<GString> for &'a GString {
type Output = GString;

fn add(self, other: GString) -> GString {
str(&[self.to_variant(), other.to_variant()])
}
}

impl<'b> Add<&'b GString> for GString {
type Output = GString;

fn add(self, other: &'b GString) -> GString {
str(&[self.to_variant(), other.to_variant()])
}
}


// ----------------------------------------------------------------------------------------------------------------------------------------------
// Conversion from/into Rust string-types

Expand Down
1 change: 1 addition & 0 deletions itest/rust/src/engine_tests/utilities_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ fn utilities_str() {

// TODO: implement GString==&str operator. Then look for "...".into() patterns and replace them.
assert_eq!(concat, "12 is a true number".into());
assert_eq!(concat, &GString::from("12") + &GString::from(" is a ") + &GString::from("true") + &GString::from(" number"));
assert_eq!(empty, GString::new());
}

Expand Down