Skip to content

Instantly share code, notes, and snippets.

@milseman
Created April 17, 2019 19:28
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 milseman/54ba2d78fff68a933df924e37bab15dd to your computer and use it in GitHub Desktop.
Save milseman/54ba2d78fff68a933df924e37bab15dd to your computer and use it in GitHub Desktop.
Magic Caret
prefix operator ^
public prefix func ^<T>(_ t: T) -> T? { return .some(t) }
//
// Numbers
//
// UInt
public prefix func ^(_ t: UInt8) -> UInt16 { return UInt16(truncatingIfNeeded: t) }
public prefix func ^(_ t: UInt8) -> UInt32 { return UInt32(truncatingIfNeeded: t) }
public prefix func ^(_ t: UInt8) -> UInt64 { return UInt64(truncatingIfNeeded: t) }
public prefix func ^(_ t: UInt8) -> UInt { return UInt(truncatingIfNeeded: t) }
public prefix func ^(_ t: UInt16) -> UInt32 { return UInt32(truncatingIfNeeded: t) }
public prefix func ^(_ t: UInt16) -> UInt64 { return UInt64(truncatingIfNeeded: t) }
public prefix func ^(_ t: UInt16) -> UInt { return UInt(truncatingIfNeeded: t) }
public prefix func ^(_ t: UInt32) -> UInt64 { return UInt64(truncatingIfNeeded: t) }
public prefix func ^(_ t: UInt32) -> UInt { return UInt(truncatingIfNeeded: t) }
#if arch(i386) || arch(arm)
#else
public prefix func ^(_ t: UInt64) -> UInt { return UInt(truncatingIfNeeded: t) }
#endif
// Int
public prefix func ^(_ t: Int8) -> Int16 { return Int16(truncatingIfNeeded: t) }
public prefix func ^(_ t: Int8) -> Int32 { return Int32(truncatingIfNeeded: t) }
public prefix func ^(_ t: Int8) -> Int64 { return Int64(truncatingIfNeeded: t) }
public prefix func ^(_ t: Int8) -> Int { return Int(truncatingIfNeeded: t) }
public prefix func ^(_ t: Int16) -> Int32 { return Int32(truncatingIfNeeded: t) }
public prefix func ^(_ t: Int16) -> Int64 { return Int64(truncatingIfNeeded: t) }
public prefix func ^(_ t: Int16) -> Int { return Int(truncatingIfNeeded: t) }
public prefix func ^(_ t: Int32) -> Int64 { return Int64(truncatingIfNeeded: t) }
public prefix func ^(_ t: Int32) -> Int { return Int(truncatingIfNeeded: t) }
#if arch(i386) || arch(arm)
#else
public prefix func ^(_ t: Int64) -> Int { return Int(truncatingIfNeeded: t) }
#endif
// Float
public prefix func ^(_ t: Float) -> Double { return Double(t) }
//
// Pointers
//
public prefix func ^<T>(_ t: UnsafeMutablePointer<T>) -> UnsafePointer<T> { return UnsafePointer(t) }
public prefix func ^<T>(_ t: UnsafeMutableBufferPointer<T>) -> UnsafeBufferPointer<T> { return UnsafeBufferPointer(t) }
public prefix func ^(_ t: UnsafePointer<CChar>) -> UnsafePointer<UInt8> { return UnsafeRawPointer(t).assumingMemoryBound(to: UInt8.self) }
public prefix func ^(_ t: UnsafeBufferPointer<CChar>) -> UnsafeBufferPointer<UInt8> {
return UnsafeBufferPointer(start: ^t.baseAddress!, count: t.count)
}
public prefix func ^(_ t: UnsafePointer<UInt8>) -> UnsafePointer<CChar> { return UnsafeRawPointer(t).assumingMemoryBound(to: CChar.self) }
public prefix func ^(_ t: UnsafeBufferPointer<UInt8>) -> UnsafeBufferPointer<CChar> {
return UnsafeBufferPointer(start: ^t.baseAddress!, count: t.count)
}
public prefix func ^<T>(_ t: Slice<UnsafeBufferPointer<T>>) -> UnsafeBufferPointer<T> { return UnsafeBufferPointer(rebasing: t) }
public prefix func ^<T>(_ t: Slice<UnsafeMutableBufferPointer<T>>) -> UnsafeMutableBufferPointer<T> {
return UnsafeMutableBufferPointer(rebasing: t)
}
//
// Slices
//
public prefix func ^<C: Collection>(_ t: C) -> C.SubSequence { return t[...] }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment