Skip to content

Instantly share code, notes, and snippets.

@jweinberg
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jweinberg/df80e0b253c06823a002 to your computer and use it in GitHub Desktop.
Save jweinberg/df80e0b253c06823a002 to your computer and use it in GitHub Desktop.
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)
}
@jtbandes
Copy link

Remove _all_ the types!

@jtbandes
Copy link

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