Skip to content

Commit

Permalink
Embrace new LosslessStringConvertible in NodePath, GString and String…
Browse files Browse the repository at this point in the history
…Name, allowing Strings to be used as constructors for these types
  • Loading branch information
migueldeicaza committed Dec 9, 2023
1 parent ec07b5d commit cc00834
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
25 changes: 24 additions & 1 deletion Generator/Generator/BuiltinGen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@ func generateBuiltinClasses (values: [JGodotBuiltinClass], outputDir: String?) a
if bc.name == "String" || bc.name == "StringName" || bc.name == "NodePath" {
conformances.append ("ExpressibleByStringLiteral")
conformances.append ("ExpressibleByStringInterpolation")
conformances.append ("LosslessStringConvertible")
}
if bc.name.starts(with: "Packed") {
conformances.append ("Collection")
Expand All @@ -500,7 +501,7 @@ func generateBuiltinClasses (values: [JGodotBuiltinClass], outputDir: String?) a

p ("public \(kind == .isStruct ? "struct" : "class") \(typeName)\(proto)") {
if bc.name == "String" {
p ("public init (_ str: String)") {
p ("public required init (_ str: String)") {
p ("gi.string_new_with_utf8_chars (&content, str)")
}
p ("// ExpressibleByStringLiteral conformace")
Expand All @@ -518,6 +519,19 @@ func generateBuiltinClasses (values: [JGodotBuiltinClass], outputDir: String?) a
p ("NodePath.constructor2 (&content, &args)")
}
}
p ("// LosslessStringConvertible conformance)")
p ("public required init (_ value: String)") {
p ("let from = GString (value)")
p ("var args: [UnsafeRawPointer?] = []")
p ("withUnsafePointer (to: &from.content)", arg: " ptr in") {
p ("args.append (ptr)")
p ("NodePath.constructor2 (&content, &args)")
}
}
p ("public var description: String") {
p ("let sub = getConcatenatedSubnames ().description")
p ("return getConcatenatedNames ().description + (sub == \"\" ? sub : \":\\(sub)\")")
}
}
if bc.name == "StringName" {
// TODO: This is a little brittle, because I am
Expand All @@ -540,6 +554,15 @@ func generateBuiltinClasses (values: [JGodotBuiltinClass], outputDir: String?) a
p ("StringName.constructor2 (&content, &args)")
}
}
p ("// LosslessStringConvertible conformance)")
p ("public required init (_ value: String)") {
p ("let from = GString (value)")
p ("var args: [UnsafeRawPointer?] = []")
p ("withUnsafePointer (to: &from.content)", arg: " ptr in"){
p ("args.append (ptr)")
p ("StringName.constructor2 (&content, &args)")
}
}
}
if bc.name == "Callable" {
p ("/// Creates a Callable instance from a Swift function")
Expand Down
5 changes: 0 additions & 5 deletions Sources/SwiftGodot/Core/StringExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@
@_implementationOnly import GDExtension

extension StringName: CustomStringConvertible {
/// Creates a StringName from a Swift String
public convenience init (_ from: String) {
self.init (from: from)
}

/// Creates a StringName from a Swift String.Substring
public convenience init (_ from: String.SubSequence) {
self.init (from: String (from))
Expand Down

0 comments on commit cc00834

Please sign in to comment.