Skip to content

Commit 604f299

Browse files
authored
Merge pull request #60759 from DevYeom/indentation_rst_files
Fix indentation of sample code in rst files.
2 parents f674b47 + f0cc6df commit 604f299

File tree

2 files changed

+100
-100
lines changed

2 files changed

+100
-100
lines changed

docs/OptimizationTips.rst

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,13 @@ in the following ``C.array1`` and ``D.array1`` will be accessed directly
134134
}
135135
136136
func usingC(_ c: C) {
137-
c.array1[i] = ... // Can directly access C.array without going through dynamic dispatch.
138-
c.doSomething() = ... // Can directly call C.doSomething without going through virtual dispatch.
137+
c.array1[i] = ... // Can directly access C.array without going through dynamic dispatch.
138+
c.doSomething() = ... // Can directly call C.doSomething without going through virtual dispatch.
139139
}
140140
141141
func usingD(_ d: D) {
142-
d.array1[i] = ... // Can directly access D.array1 without going through dynamic dispatch.
143-
d.array2[i] = ... // Will access D.array2 through dynamic dispatch.
142+
d.array1[i] = ... // Can directly access D.array1 without going through dynamic dispatch.
143+
d.array2[i] = ... // Will access D.array2 through dynamic dispatch.
144144
}
145145
146146
Advice: Use 'private' and 'fileprivate' when declaration does not need to be accessed outside of file
@@ -465,23 +465,23 @@ construct such a data structure:
465465
466466
final class Ref<T> {
467467
var val: T
468-
init(_ v: T) {val = v}
468+
init(_ v: T) { val = v }
469469
}
470470
471471
struct Box<T> {
472-
var ref: Ref<T>
473-
init(_ x: T) { ref = Ref(x) }
474-
475-
var value: T {
476-
get { return ref.val }
477-
set {
478-
if !isKnownUniquelyReferenced(&ref) {
479-
ref = Ref(newValue)
480-
return
481-
}
482-
ref.val = newValue
483-
}
472+
var ref: Ref<T>
473+
init(_ x: T) { ref = Ref(x) }
474+
475+
var value: T {
476+
get { return ref.val }
477+
set {
478+
if !isKnownUniquelyReferenced(&ref) {
479+
ref = Ref(newValue)
480+
return
481+
}
482+
ref.val = newValue
484483
}
484+
}
485485
}
486486
487487
The type ``Box`` can replace the array in the code sample above.
@@ -501,9 +501,9 @@ count operations are expensive and unavoidable when using Swift classes.
501501
.. code-block:: swift
502502
503503
final class Node {
504-
var next: Node?
505-
var data: Int
506-
...
504+
var next: Node?
505+
var data: Int
506+
...
507507
}
508508
509509
@@ -525,27 +525,27 @@ alive.
525525

526526
.. code-block:: swift
527527
528-
// The call to ``withExtendedLifetime(Head)`` makes sure that the lifetime of
529-
// Head is guaranteed to extend over the region of code that uses Unmanaged
530-
// references. Because there exists a reference to Head for the duration
531-
// of the scope and we don't modify the list of ``Node``s there also exist a
532-
// reference through the chain of ``Head.next``, ``Head.next.next``, ...
533-
// instances.
528+
// The call to ``withExtendedLifetime(Head)`` makes sure that the lifetime of
529+
// Head is guaranteed to extend over the region of code that uses Unmanaged
530+
// references. Because there exists a reference to Head for the duration
531+
// of the scope and we don't modify the list of ``Node``s there also exist a
532+
// reference through the chain of ``Head.next``, ``Head.next.next``, ...
533+
// instances.
534534
535-
withExtendedLifetime(Head) {
535+
withExtendedLifetime(Head) {
536536
537-
// Create an Unmanaged reference.
538-
var Ref: Unmanaged<Node> = Unmanaged.passUnretained(Head)
537+
// Create an Unmanaged reference.
538+
var Ref: Unmanaged<Node> = Unmanaged.passUnretained(Head)
539539
540-
// Use the unmanaged reference in a call/variable access. The use of
541-
// _withUnsafeGuaranteedRef allows the compiler to remove the ultimate
542-
// retain/release across the call/access.
540+
// Use the unmanaged reference in a call/variable access. The use of
541+
// _withUnsafeGuaranteedRef allows the compiler to remove the ultimate
542+
// retain/release across the call/access.
543543
544-
while let Next = Ref._withUnsafeGuaranteedRef { $0.next } {
545-
...
546-
Ref = Unmanaged.passUnretained(Next)
547-
}
544+
while let Next = Ref._withUnsafeGuaranteedRef { $0.next } {
545+
...
546+
Ref = Unmanaged.passUnretained(Next)
548547
}
548+
}
549549
550550
551551
.. _Unmanaged.swift: https://github.com/apple/swift/blob/main/stdlib/public/core/Unmanaged.swift

docs/proposals/Concurrency.rst

Lines changed: 64 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,17 @@ multi-threaded program below.
4646

4747
.. code-block:: swift
4848
49-
import Foundation
49+
import Foundation
5050
51-
let queue = DispatchQueue.global(qos: .default)
51+
let queue = DispatchQueue.global(qos: .default)
5252
53-
class Bird {}
54-
var single = Bird()
53+
class Bird {}
54+
var single = Bird()
5555
56-
queue.async {
57-
while true { single = Bird() }
58-
}
56+
queue.async {
5957
while true { single = Bird() }
58+
}
59+
while true { single = Bird() }
6060
6161
This program crashes very quickly when it tries to deallocate an already
6262
deallocated class instance. To understand the bug try to imagine two threads
@@ -223,13 +223,13 @@ be called by worker-threads.
223223

224224
.. code-block:: swift
225225
226-
func logger(_ x : Int) {
226+
func logger(_ x : Int) {
227227
228-
// I know what I'm doing!
229-
issafe {
230-
glob = x
231-
}
228+
// I know what I'm doing!
229+
issafe {
230+
glob = x
232231
}
232+
}
233233
234234
235235
Most protocols in the standard library, like `Incrementable` and `Equatable` are
@@ -324,20 +324,20 @@ This is an example of a tiny concurrent program that uses Tasks and Streams.
324324

325325
.. code-block:: swift
326326
327-
let input = Stream<String>()
328-
let output = Stream<String>()
327+
let input = Stream<String>()
328+
let output = Stream<String>()
329329
330-
func echoServer(_ inp : Stream<String>,
331-
out : Stream<String>) {
332-
while true { out.push(inp.pop()) }
333-
}
330+
func echoServer(_ inp : Stream<String>,
331+
out : Stream<String>) {
332+
while true { out.push(inp.pop()) }
333+
}
334334
335-
createTask((input, output), callback: echoServer)
335+
createTask((input, output), callback: echoServer)
336336
337-
for val in ["hello","world"] {
338-
input.push(val)
339-
print(output.pop())
340-
}
337+
for val in ["hello","world"] {
338+
input.push(val)
339+
print(output.pop())
340+
}
341341
342342
The program above creates a server task that accepts an input stream and an
343343
output stream that allows it to communicate with the main thread. The compiler
@@ -352,20 +352,20 @@ declaration of the streams in the closure.
352352

353353
.. code-block:: swift
354354
355-
let comm : _Endpoint<String, Int> = createTask {
356-
var counter = 0
357-
while true {
358-
$0.pop()
359-
$0.push(counter)
360-
counter += 1
361-
}
362-
}
363-
364-
// CHECK: 0, 1, 2,
365-
for ss in ["","",""] {
366-
comm.push(ss)
367-
print("\(comm.pop()), ", terminator: "")
368-
}
355+
let comm : _Endpoint<String, Int> = createTask {
356+
var counter = 0
357+
while true {
358+
$0.pop()
359+
$0.push(counter)
360+
counter += 1
361+
}
362+
}
363+
364+
// CHECK: 0, 1, 2,
365+
for ss in ["","",""] {
366+
comm.push(ss)
367+
print("\(comm.pop()), ", terminator: "")
368+
}
369369
370370
Stream utilities
371371
----------------
@@ -403,19 +403,19 @@ Example of a concurrent program using Futures in Swift.
403403

404404
.. code-block:: swift
405405
406-
func mergeSort<T : Comparable>(array: ArraySlice<T>) -> [T] {
406+
func mergeSort<T : Comparable>(array: ArraySlice<T>) -> [T] {
407407
408-
if array.count <= 16 { return Array(array).sorted() }
408+
if array.count <= 16 { return Array(array).sorted() }
409409
410-
let mid = array.count / 2
411-
let left = array[0..<mid]
412-
let right = array[mid..<array.count]
410+
let mid = array.count / 2
411+
let left = array[0..<mid]
412+
let right = array[mid..<array.count]
413413
414-
let lf = async(left, callback: mergeSort)
415-
let lr = async(right, callback: mergeSort)
414+
let lf = async(left, callback: mergeSort)
415+
let lr = async(right, callback: mergeSort)
416416
417-
return merge(lf.await(), lr.await())
418-
}
417+
return merge(lf.await(), lr.await())
418+
}
419419
420420
The program above uses async to execute two tasks that sorts the two halves of
421421
the array in parallel. Notice that the arrays in the example above are not
@@ -428,21 +428,21 @@ Here is another example of async calls using trailing closures and enums.
428428

429429
.. code-block:: swift
430430
431-
enum Shape {
432-
case circle, oval, square, triangle
433-
}
431+
enum Shape {
432+
case circle, oval, square, triangle
433+
}
434434
435-
let res = async(Shape.oval) { (c: Shape) -> String in
436-
switch c {
437-
case .circle: return "Circle"
438-
case .oval: return "Oval"
439-
case .square: return "Square"
440-
case .triangle: return "Triangle"
441-
}
442-
}
435+
let res = async(Shape.oval) { (c: Shape) -> String in
436+
switch c {
437+
case .circle: return "Circle"
438+
case .oval: return "Oval"
439+
case .square: return "Square"
440+
case .triangle: return "Triangle"
441+
}
442+
}
443443
444-
//CHECK: Shape: Oval
445-
print("Shape: \(res.await())")
444+
//CHECK: Shape: Oval
445+
print("Shape: \(res.await())")
446446
447447
Notice that the swift compiler infers that ``Shape`` and `String` can be sent
448448
between the threads.
@@ -520,11 +520,11 @@ thread-safe (explained in the previous section). For example:
520520

521521
.. code-block:: swift
522522
523-
@_semantics("swift.concurrent.async")
524-
// This annotation tells the compiler to verify the closure and the passed arguments at the call site.
525-
public func async<RetTy, ArgsTy>(args: ArgsTy, callback: @escaping (ArgsTy) -> RetTy) -> Future<RetTy> {
526-
return unsafeAsync(args, callback: callback)
527-
}
523+
@_semantics("swift.concurrent.async")
524+
// This annotation tells the compiler to verify the closure and the passed arguments at the call site.
525+
public func async<RetTy, ArgsTy>(args: ArgsTy, callback: @escaping (ArgsTy) -> RetTy) -> Future<RetTy> {
526+
return unsafeAsync(args, callback: callback)
527+
}
528528
529529
Example of shared data structures
530530
---------------------------------

0 commit comments

Comments
 (0)