Browse Source

change the implement way of namespace extension

Carl Chen 9 years ago
parent
commit
b911efce5b
3 changed files with 52 additions and 31 deletions
  1. 1 1
      Kingfisher.swift
  2. 31 19
      Sources/Image.swift
  3. 20 11
      Sources/String+MD5.swift

+ 1 - 1
Kingfisher.swift

@@ -37,7 +37,7 @@ public struct Kingfisher<Base> {
  */
 public protocol KingfisherCompatible {
     associatedtype CompatibleType
-    var kf: Kingfisher<CompatibleType> { get set }
+    var kf: CompatibleType { get }
 }
 
 public extension KingfisherCompatible {

+ 31 - 19
Sources/Image.swift

@@ -651,20 +651,24 @@ enum ImageFormat {
 
 
 // MARK: - Misc Helpers
-protocol DataType {
-    func getBytes(_ buffer: UnsafeMutableRawPointer, length: Int)
+public struct DataProxy {
+    fileprivate let base: Data
+    init(proxy: Data) {
+        base = proxy
+    }
 }
-extension Data: DataType {
-    func getBytes(_ buffer: UnsafeMutableRawPointer, length: Int) {
-        (self as NSData).getBytes(buffer, length: length)
+
+extension Data: KingfisherCompatible {
+    public typealias CompatibleType = DataProxy
+    public var kf: DataProxy {
+        return DataProxy(proxy: self)
     }
 }
 
-extension Data: KingfisherCompatible { }
-extension Kingfisher where Base: DataType {
+extension DataProxy {
     var imageFormat: ImageFormat {
         var buffer = [UInt8](repeating: 0, count: 8)
-        base.getBytes(&buffer, length: 8)
+        (base as NSData).getBytes(&buffer, length: 8)
         if buffer == ImageHeaderData.PNG {
             return .PNG
         } else if buffer[0] == ImageHeaderData.JPEG_SOI[0] &&
@@ -678,38 +682,46 @@ extension Kingfisher where Base: DataType {
         {
             return .GIF
         }
-        
+
         return .unknown
     }
 }
 
-protocol SizeType {
-    var width: CGFloat { get }
-    var height: CGFloat { get }
+public struct CGSizeProxy {
+    fileprivate let base: CGSize
+    init(proxy: CGSize) {
+        base = proxy
+    }
 }
-extension CGSize: SizeType { }
 
-extension CGSize: KingfisherCompatible { }
-extension Kingfisher where Base: SizeType {
+extension CGSize: KingfisherCompatible {
+    public typealias CompatibleType = CGSizeProxy
+    public var kf: CGSizeProxy {
+        return CGSizeProxy(proxy: self)
+    }
+}
+
+extension CGSizeProxy {
     func constrained(_ size: CGSize) -> CGSize {
         let aspectWidth = round(aspectRatio * size.height)
         let aspectHeight = round(size.width / aspectRatio)
-        
+
         return aspectWidth > size.width ? CGSize(width: size.width, height: aspectHeight) : CGSize(width: aspectWidth, height: size.height)
     }
-    
+
     func filling(_ size: CGSize) -> CGSize {
         let aspectWidth = round(aspectRatio * size.height)
         let aspectHeight = round(size.width / aspectRatio)
-        
+
         return aspectWidth < size.width ? CGSize(width: size.width, height: aspectHeight) : CGSize(width: aspectWidth, height: size.height)
     }
-    
+
     private var aspectRatio: CGFloat {
         return base.height == 0.0 ? 1.0 : base.width / base.height
     }
 }
 
+
 extension CGImage {
     var isARGB8888: Bool {
         return bitsPerPixel == 32 && bitsPerComponent == 8 && bitmapInfo.contains(.alphaInfoMask)

+ 20 - 11
Sources/String+MD5.swift

@@ -21,35 +21,44 @@ Permission is granted to anyone to use this software for any purpose,including c
 
 import Foundation
 
-protocol StringType {
-    func data(using encoding: String.Encoding, allowLossyConversion: Bool) -> Data?
+public struct StringProxy {
+    fileprivate let base: String
+    init(proxy: String) {
+        base = proxy
+    }
+}
+
+extension String: KingfisherCompatible {
+    public typealias CompatibleType = StringProxy
+    public var kf: CompatibleType {
+        return StringProxy(proxy: self)
+    }
 }
-extension String: StringType { }
-extension String: KingfisherCompatible {}
 
-extension Kingfisher where Base: StringType {
-    var md5: Base {
+extension StringProxy {
+    var md5: String {
         if let data = base.data(using: .utf8, allowLossyConversion: true) {
-            
+
             let message = data.withUnsafeBytes { bytes -> [UInt8] in
                 return Array(UnsafeBufferPointer(start: bytes, count: data.count))
             }
-            
+
             let MD5Calculator = MD5(message)
             let MD5Data = MD5Calculator.calculate()
-            
+
             let MD5String = NSMutableString()
             for c in MD5Data {
                 MD5String.appendFormat("%02x", c)
             }
-            return MD5String as! Base
-            
+            return MD5String as String
+
         } else {
             return base
         }
     }
 }
 
+
 /** array of bytes, little-endian representation */
 func arrayOfBytes<T>(_ value: T, length: Int? = nil) -> [UInt8] {
     let totalBytes = length ?? (MemoryLayout<T>.size * 8)