Skip to content
Permalink
Browse files

Conform CircularBuffer to ExpressibleByArrayLiteral (#1102)

Motivation:
Making CircularBuffer conform to ExpressibleByArrayLiteral enables
directly initializing it from an array literal, which can sometimes
make its use simpler and more natural, for instance:
foo.circularBuffer = [1,2,3] // vs `= CircularBuffer([1,2,3])`
Foo(circularBuffer: [a, b, c]) // vs `: CircularBuffer([a, b, c])`
This is consistent with other standard Swift collections, such as
Set, as well as existing NIO types (eg: WebSocketMaskingKey).

Modifications:
Extend CircularBuffer to conform to ExpressibleByArrayLiteral

Result:
CircularBuffer can now be initialized from array literals
  • Loading branch information...
adtrevor authored and Lukasa committed Aug 12, 2019
1 parent dcb3099 commit a28a8668b5ae1c3cdce38b4bffc3bf06e271ebe0
@@ -706,3 +706,9 @@ extension CircularBuffer: Hashable where Element: Hashable {
}
}
}

extension CircularBuffer: ExpressibleByArrayLiteral {
public init(arrayLiteral elements: Element...) {
self.init(elements)
}
}
@@ -75,6 +75,7 @@ extension CircularBufferTests {
("testModify", testModify),
("testEquality", testEquality),
("testHash", testHash),
("testArrayLiteralInit", testArrayLiteralInit),
]
}
}
@@ -971,4 +971,17 @@ class CircularBufferTests: XCTestCase {
XCTAssertEqual(Set([prependBuff,appendBuff]).count, 1)
}

func testArrayLiteralInit() {
let empty: CircularBuffer<Int> = []
XCTAssert(empty.isEmpty)

let increasingInts: CircularBuffer = [1, 2, 3, 4, 5]
XCTAssertEqual(increasingInts.count, 5)
XCTAssert(zip(increasingInts, 1...5).allSatisfy(==))

let someIntsArray = [-9, 384, 2, 10, 0, 0, 0]
let someInts: CircularBuffer = [-9, 384, 2, 10, 0, 0, 0]
XCTAssertEqual(someInts.count, 7)
XCTAssert(zip(someInts, someIntsArray).allSatisfy(==))
}
}

0 comments on commit a28a866

Please sign in to comment.
You can’t perform that action at this time.