Last active
August 29, 2015 14:02
-
-
Save jweinberg/df80e0b253c06823a002 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct ReinterpretSequence<T, U> : Swift.Collection | |
{ | |
typealias IndexType = Int | |
typealias GeneratorType = IndexingGenerator<ReinterpretSequence<T,U>> | |
let value: T | |
var startIndex: IndexType { return 0 } | |
var endIndex: IndexType { return sizeof(T) / sizeof(U) } | |
subscript (i: IndexType) -> U { | |
var o = value | |
return withUnsafePointer(&o) { UnsafePointer($0.value)[i] } | |
} | |
func generate() -> GeneratorType { | |
return IndexingGenerator(self) | |
} | |
} | |
let seq: ReinterpretSequence<UInt32, CChar> = ReinterpretSequence(value: 0x74736574) | |
for s in seq { | |
println(s) | |
} |
I think you can just use ...>(var t: T, index: Int)
rather than inout
; this also reduces the need for var
and &
elsewhere.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Remove _all_ the types!