Skip to content

Commit 177bd24

Browse files
authored
Merge pull request #30702 from benrimmington/static-string-extensions
[stdlib] StaticString: extension per conformance
2 parents 1c2bc06 + 6ad83a0 commit 177bd24

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

stdlib/public/core/StaticString.swift

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
1010
//
1111
//===----------------------------------------------------------------------===//
12-
12+
//
1313
// Implementation Note: Because StaticString is used in the
1414
// implementation of _precondition(), _fatalErrorMessage(), etc., we
1515
// keep it extremely close to the bare metal. In particular, because
1616
// we store only Builtin types, we are guaranteed that no assertions
1717
// are involved in its construction. This feature is crucial for
1818
// preventing infinite recursion even in non-asserting cases.
19+
//
20+
//===----------------------------------------------------------------------===//
1921

2022
/// A string type designed to represent text that is known at compile time.
2123
///
@@ -67,16 +69,7 @@
6769
/// utf8[4] //-> Fatal error!
6870
/// }
6971
@frozen
70-
public struct StaticString
71-
: _ExpressibleByBuiltinUnicodeScalarLiteral,
72-
_ExpressibleByBuiltinExtendedGraphemeClusterLiteral,
73-
_ExpressibleByBuiltinStringLiteral,
74-
ExpressibleByUnicodeScalarLiteral,
75-
ExpressibleByExtendedGraphemeClusterLiteral,
76-
ExpressibleByStringLiteral,
77-
CustomStringConvertible,
78-
CustomDebugStringConvertible,
79-
CustomReflectable {
72+
public struct StaticString {
8073

8174
/// Either a pointer to the start of UTF-8 data, represented as an integer,
8275
/// or an integer representation of a single Unicode scalar.
@@ -228,12 +221,18 @@ public struct StaticString
228221
? (0x3 as UInt8)._value
229222
: (0x1 as UInt8)._value
230223
}
224+
}
225+
226+
extension StaticString: _ExpressibleByBuiltinUnicodeScalarLiteral {
231227

232228
@_effects(readonly)
233229
@_transparent
234230
public init(_builtinUnicodeScalarLiteral value: Builtin.Int32) {
235231
self = StaticString(unicodeScalar: value)
236232
}
233+
}
234+
235+
extension StaticString: ExpressibleByUnicodeScalarLiteral {
237236

238237
/// Creates an instance initialized to a single Unicode scalar.
239238
///
@@ -244,6 +243,9 @@ public struct StaticString
244243
public init(unicodeScalarLiteral value: StaticString) {
245244
self = value
246245
}
246+
}
247+
248+
extension StaticString: _ExpressibleByBuiltinExtendedGraphemeClusterLiteral {
247249

248250
@_effects(readonly)
249251
@_transparent
@@ -258,6 +260,9 @@ public struct StaticString
258260
isASCII: isASCII
259261
)
260262
}
263+
}
264+
265+
extension StaticString: ExpressibleByExtendedGraphemeClusterLiteral {
261266

262267
/// Creates an instance initialized to a single character that is made up of
263268
/// one or more Unicode scalar values.
@@ -269,6 +274,9 @@ public struct StaticString
269274
public init(extendedGraphemeClusterLiteral value: StaticString) {
270275
self = value
271276
}
277+
}
278+
279+
extension StaticString: _ExpressibleByBuiltinStringLiteral {
272280

273281
@_effects(readonly)
274282
@_transparent
@@ -282,6 +290,9 @@ public struct StaticString
282290
utf8CodeUnitCount: utf8CodeUnitCount,
283291
isASCII: isASCII)
284292
}
293+
}
294+
295+
extension StaticString: ExpressibleByStringLiteral {
285296

286297
/// Creates an instance initialized to the value of a string literal.
287298
///
@@ -292,19 +303,26 @@ public struct StaticString
292303
public init(stringLiteral value: StaticString) {
293304
self = value
294305
}
306+
}
307+
308+
extension StaticString: CustomStringConvertible {
295309

296310
/// A textual representation of the static string.
297311
public var description: String {
298312
return withUTF8Buffer { String._uncheckedFromUTF8($0) }
299313
}
314+
}
315+
316+
extension StaticString: CustomDebugStringConvertible {
300317

301318
/// A textual representation of the static string, suitable for debugging.
302319
public var debugDescription: String {
303320
return self.description.debugDescription
304321
}
305322
}
306323

307-
extension StaticString {
324+
extension StaticString: CustomReflectable {
325+
308326
public var customMirror: Mirror {
309327
return Mirror(reflecting: description)
310328
}

0 commit comments

Comments
 (0)