Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply existential any to protocol for Swift 6 #2283

Merged
merged 2 commits into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ protocol KingfisherActionAlertPopup {
}
}

@MainActor func reloadAction(_ reloadable: MainDataViewReloadable) -> UIAlertAction {
@MainActor func reloadAction(_ reloadable: any MainDataViewReloadable) -> UIAlertAction {
return UIAlertAction(title: "Reload", style: .default) { _ in
reloadable.reload()
}
Expand All @@ -76,7 +76,7 @@ protocol KingfisherActionAlertPopup {
extension UIViewController: KingfisherActionAlertPopup {
@objc func alertPopup(_ sender: Any) -> UIAlertController {
let alert = createAlert(sender, actions: [cleanCacheAction(), cancelAction])
if let r = self as? MainDataViewReloadable {
if let r = self as? any MainDataViewReloadable {
alert.addAction(reloadAction(r))
}
return alert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ struct UserNameLetterIconImageProvider: ImageDataProvider {
color = backgroundColor
}

func data(handler: @escaping (Result<Data, Error>) -> Void) {
func data(handler: @escaping (Result<Data, any Error>) -> Void) {
let letter = self.letter as NSString
let rect = CGRect(x: 0, y: 0, width: 250, height: 250)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ private let reuseIdentifier = "ProcessorCell"

class ProcessorCollectionViewController: UICollectionViewController {

var currentProcessor: ImageProcessor = DefaultImageProcessor.default {
var currentProcessor: any ImageProcessor = DefaultImageProcessor.default {
didSet {
collectionView.reloadData()
}
}

var processors: [(ImageProcessor, String)] = [
var processors: [(any ImageProcessor, String)] = [
(DefaultImageProcessor.default, "Default"),
(ResizingImageProcessor(referenceSize: CGSize(width: 50, height: 50)), "Resizing"),
(RoundCornerImageProcessor(radius: .point(20)), "Round Corner"),
Expand Down
8 changes: 4 additions & 4 deletions Sources/Cache/ImageCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ open class ImageCache: @unchecked Sendable {
original: Data? = nil,
forKey key: String,
processorIdentifier identifier: String = "",
cacheSerializer serializer: CacheSerializer = DefaultCacheSerializer.default,
cacheSerializer serializer: any CacheSerializer = DefaultCacheSerializer.default,
toDisk: Bool = true,
callbackQueue: CallbackQueue = .untouch,
completionHandler: (@Sendable (CacheStoreResult) -> Void)? = nil
Expand Down Expand Up @@ -522,15 +522,15 @@ open class ImageCache: @unchecked Sendable {
fromMemory: Bool = true,
fromDisk: Bool = true,
callbackQueue: CallbackQueue = .untouch,
completionHandler: (@Sendable (Error?) -> Void)? = nil)
completionHandler: (@Sendable ((any Error)?) -> Void)? = nil)
{
let computedKey = key.computedKey(with: identifier)

if fromMemory {
memoryStorage.remove(forKey: computedKey)
}

@Sendable func callHandler(_ error: Error?) {
@Sendable func callHandler(_ error: (any Error)?) {
if let completionHandler = completionHandler {
callbackQueue.execute { completionHandler(error) }
}
Expand Down Expand Up @@ -1004,7 +1004,7 @@ open class ImageCache: @unchecked Sendable {
original: Data? = nil,
forKey key: String,
processorIdentifier identifier: String = "",
cacheSerializer serializer: CacheSerializer = DefaultCacheSerializer.default,
cacheSerializer serializer: any CacheSerializer = DefaultCacheSerializer.default,
toDisk: Bool = true
) async throws {
try await withCheckedThrowingContinuation { continuation in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ struct UserNameLetterIconImageProvider: ImageDataProvider {
self.letter = userNameFirstLetter
}

func data(handler: @escaping (Result<Data, Error>) -> Void) {
func data(handler: @escaping (Result<Data, any Error>) -> Void) {

// You can ignore these detail below.
// It generates some data for an image with `letter` being rendered in the center.
Expand Down
2 changes: 1 addition & 1 deletion Sources/Extensions/CPListItem+Kingfisher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ extension KingfisherWrapper where Base: CPListItem {
///
@discardableResult
public func setImage(
with resource: Resource?,
with resource: (any Resource)?,
placeholder: KFCrossPlatformImage? = nil,
options: KingfisherOptionsInfo? = nil,
progressBlock: DownloadProgressBlock? = nil,
Expand Down
8 changes: 4 additions & 4 deletions Sources/Extensions/HasImageComponent+Kingfisher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ extension KingfisherWrapper where Base: KingfisherImageSettable {
/// > Both `progressBlock` and `completionHandler` will also be executed in the main thread.
@discardableResult
public func setImage(
with resource: Resource?,
with resource: (any Resource)?,
placeholder: KFCrossPlatformImage? = nil,
options: KingfisherOptionsInfo? = nil,
progressBlock: DownloadProgressBlock? = nil,
Expand Down Expand Up @@ -275,7 +275,7 @@ extension KingfisherWrapper where Base: KingfisherImageSettable {
/// > Both `progressBlock` and `completionHandler` will also be executed in the main thread.
@discardableResult
public func setImage(
with resource: Resource?,
with resource: (any Resource)?,
placeholder: KFCrossPlatformImage? = nil,
options: KingfisherOptionsInfo? = nil,
completionHandler: (@MainActor @Sendable (Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil
Expand Down Expand Up @@ -307,7 +307,7 @@ extension KingfisherWrapper where Base: KingfisherImageSettable {
/// > Both `progressBlock` and `completionHandler` will also be executed in the main thread.
@discardableResult
public func setImage(
with provider: ImageDataProvider?,
with provider: (any ImageDataProvider)?,
placeholder: KFCrossPlatformImage? = nil,
options: KingfisherOptionsInfo? = nil,
progressBlock: DownloadProgressBlock? = nil,
Expand Down Expand Up @@ -338,7 +338,7 @@ extension KingfisherWrapper where Base: KingfisherImageSettable {
/// > Both `progressBlock` and `completionHandler` will also be executed in the main thread.
@discardableResult
public func setImage(
with provider: ImageDataProvider?,
with provider: (any ImageDataProvider)?,
placeholder: KFCrossPlatformImage? = nil,
options: KingfisherOptionsInfo? = nil,
completionHandler: (@MainActor @Sendable (Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil
Expand Down
28 changes: 14 additions & 14 deletions Sources/Extensions/ImageView+Kingfisher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ extension KingfisherWrapper where Base: KFCrossPlatformImageView {
@discardableResult
public func setImage(
with source: Source?,
placeholder: Placeholder? = nil,
placeholder: (any Placeholder)? = nil,
options: KingfisherOptionsInfo? = nil,
progressBlock: DownloadProgressBlock? = nil,
completionHandler: (@MainActor @Sendable (Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil
Expand Down Expand Up @@ -123,7 +123,7 @@ extension KingfisherWrapper where Base: KFCrossPlatformImageView {
@discardableResult
public func setImage(
with source: Source?,
placeholder: Placeholder? = nil,
placeholder: (any Placeholder)? = nil,
options: KingfisherOptionsInfo? = nil,
completionHandler: (@MainActor @Sendable (Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil
) -> DownloadTask?
Expand Down Expand Up @@ -163,8 +163,8 @@ extension KingfisherWrapper where Base: KFCrossPlatformImageView {
/// > Both `progressBlock` and `completionHandler` will also be executed in the main thread.
@discardableResult
public func setImage(
with resource: Resource?,
placeholder: Placeholder? = nil,
with resource: (any Resource)?,
placeholder: (any Placeholder)? = nil,
options: KingfisherOptionsInfo? = nil,
progressBlock: DownloadProgressBlock? = nil,
completionHandler: (@MainActor @Sendable (Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil
Expand Down Expand Up @@ -202,8 +202,8 @@ extension KingfisherWrapper where Base: KFCrossPlatformImageView {
/// > Both `progressBlock` and `completionHandler` will also be executed in the main thread.
@discardableResult
public func setImage(
with resource: Resource?,
placeholder: Placeholder? = nil,
with resource: (any Resource)?,
placeholder: (any Placeholder)? = nil,
options: KingfisherOptionsInfo? = nil,
completionHandler: (@MainActor @Sendable (Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil
) -> DownloadTask?
Expand Down Expand Up @@ -234,8 +234,8 @@ extension KingfisherWrapper where Base: KFCrossPlatformImageView {
/// > Both `progressBlock` and `completionHandler` will also be executed in the main thread.
@discardableResult
public func setImage(
with provider: ImageDataProvider?,
placeholder: Placeholder? = nil,
with provider: (any ImageDataProvider)?,
placeholder: (any Placeholder)? = nil,
options: KingfisherOptionsInfo? = nil,
progressBlock: DownloadProgressBlock? = nil,
completionHandler: (@MainActor @Sendable (Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil
Expand Down Expand Up @@ -264,8 +264,8 @@ extension KingfisherWrapper where Base: KFCrossPlatformImageView {
/// > Both `progressBlock` and `completionHandler` will also be executed in the main thread.
@discardableResult
public func setImage(
with provider: ImageDataProvider?,
placeholder: Placeholder? = nil,
with provider: (any ImageDataProvider)?,
placeholder: (any Placeholder)? = nil,
options: KingfisherOptionsInfo? = nil,
completionHandler: (@MainActor @Sendable (Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil
) -> DownloadTask?
Expand All @@ -281,7 +281,7 @@ extension KingfisherWrapper where Base: KFCrossPlatformImageView {

func setImage(
with source: Source?,
placeholder: Placeholder? = nil,
placeholder: (any Placeholder)? = nil,
parsedOptions: KingfisherParsedOptionsInfo,
progressBlock: DownloadProgressBlock? = nil,
completionHandler: (@MainActor @Sendable (Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil
Expand Down Expand Up @@ -471,9 +471,9 @@ extension KingfisherWrapper where Base: KFCrossPlatformImageView {
///
/// The protocol `Indicator` has a `view` property that will be shown when loading an image.
/// It will be `nil` if ``KingfisherWrapper/indicatorType`` is ``IndicatorType/none``.
public private(set) var indicator: Indicator? {
public private(set) var indicator: (any Indicator)? {
get {
let box: Box<Indicator>? = getAssociatedObject(base, &indicatorKey)
let box: Box<any Indicator>? = getAssociatedObject(base, &indicatorKey)
return box?.value
}

Expand Down Expand Up @@ -524,7 +524,7 @@ extension KingfisherWrapper where Base: KFCrossPlatformImageView {
/// Represents the ``Placeholder`` used for this image view.
///
/// A ``Placeholder`` will be shown in the view while it is downloading an image.
public private(set) var placeholder: Placeholder? {
public private(set) var placeholder: (any Placeholder)? {
get { return getAssociatedObject(base, &placeholderKey) }
set {
if let previousPlaceholder = placeholder {
Expand Down
4 changes: 2 additions & 2 deletions Sources/Extensions/NSButton+Kingfisher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ extension KingfisherWrapper where Base: NSButton {
/// > Both `progressBlock` and `completionHandler` will also be executed in the main thread.
@discardableResult
public func setImage(
with resource: Resource?,
with resource: (any Resource)?,
placeholder: KFCrossPlatformImage? = nil,
options: KingfisherOptionsInfo? = nil,
progressBlock: DownloadProgressBlock? = nil,
Expand Down Expand Up @@ -173,7 +173,7 @@ extension KingfisherWrapper where Base: NSButton {
/// > Both `progressBlock` and `completionHandler` will also be executed in the main thread.
@discardableResult
public func setAlternateImage(
with resource: Resource?,
with resource: (any Resource)?,
placeholder: KFCrossPlatformImage? = nil,
options: KingfisherOptionsInfo? = nil,
progressBlock: DownloadProgressBlock? = nil,
Expand Down
2 changes: 1 addition & 1 deletion Sources/Extensions/NSTextAttachment+Kingfisher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ extension KingfisherWrapper where Base: NSTextAttachment {
/// ```
@discardableResult
public func setImage(
with resource: Resource?,
with resource: (any Resource)?,
attributedView: @autoclosure @escaping @Sendable () -> KFCrossPlatformView,
placeholder: KFCrossPlatformImage? = nil,
options: KingfisherOptionsInfo? = nil,
Expand Down
4 changes: 2 additions & 2 deletions Sources/Extensions/UIButton+Kingfisher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ extension KingfisherWrapper where Base: UIButton {
///
@discardableResult
public func setImage(
with resource: Resource?,
with resource: (any Resource)?,
for state: UIControl.State,
placeholder: UIImage? = nil,
options: KingfisherOptionsInfo? = nil,
Expand Down Expand Up @@ -199,7 +199,7 @@ extension KingfisherWrapper where Base: UIButton {
///
@discardableResult
public func setBackgroundImage(
with resource: Resource?,
with resource: (any Resource)?,
for state: UIControl.State,
placeholder: UIImage? = nil,
options: KingfisherOptionsInfo? = nil,
Expand Down
2 changes: 1 addition & 1 deletion Sources/General/ImageSource/AVAssetImageDataProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public struct AVAssetImageDataProvider: ImageDataProvider {
self.init(assetURL: assetURL, time: time)
}

public func data(handler: @Sendable @escaping (Result<Data, Error>) -> Void) {
public func data(handler: @Sendable @escaping (Result<Data, any Error>) -> Void) {
assetImageGenerator.generateCGImagesAsynchronously(forTimes: [NSValue(time: time)]) {
(requestedTime, image, imageTime, result, error) in
if let error = error {
Expand Down
8 changes: 4 additions & 4 deletions Sources/General/ImageSource/ImageDataProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public protocol ImageDataProvider: Sendable {
/// - Note: If the `handler` is called with a `.failure` with error,
/// a ``KingfisherError/ImageSettingErrorReason/dataProviderError(provider:error:)`` will be finally thrown out to
/// you as the ``KingfisherError`` from the framework.
func data(handler: @escaping @Sendable (Result<Data, Error>) -> Void)
func data(handler: @escaping @Sendable (Result<Data, any Error>) -> Void)

/// The content URL represents this provider, if exists.
var contentURL: URL? { get }
Expand Down Expand Up @@ -96,7 +96,7 @@ public struct LocalFileImageDataProvider: ImageDataProvider {
/// The key used in cache.
public var cacheKey: String

public func data(handler: @escaping @Sendable (Result<Data, Error>) -> Void) {
public func data(handler: @escaping @Sendable (Result<Data, any Error>) -> Void) {
loadingQueue.execute {
handler(Result(catching: { try Data(contentsOf: fileURL) }))
}
Expand Down Expand Up @@ -147,7 +147,7 @@ public struct Base64ImageDataProvider: ImageDataProvider {
/// The key used in cache.
public var cacheKey: String

public func data(handler: (Result<Data, Error>) -> Void) {
public func data(handler: (Result<Data, any Error>) -> Void) {
let data = Data(base64Encoded: base64String)!
handler(.success(data))
}
Expand Down Expand Up @@ -178,7 +178,7 @@ public struct RawImageDataProvider: ImageDataProvider {
/// The key used in cache.
public var cacheKey: String

public func data(handler: @escaping (Result<Data, Error>) -> Void) {
public func data(handler: @escaping (Result<Data, any Error>) -> Void) {
handler(.success(data))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public struct PHPickerResultImageDataProvider: ImageDataProvider {
/// - invalidImage: The retrieved image is invalid.
public enum PHPickerResultImageDataProviderError: Error {
/// An error happens during picking up image through the item provider of `PHPickerResult`.
case pickerProviderError(Error)
case pickerProviderError(any Error)
/// The retrieved image is invalid.
case invalidImage
}
Expand Down Expand Up @@ -74,7 +74,7 @@ public struct PHPickerResultImageDataProvider: ImageDataProvider {
self.contentType = contentType
}

public func data(handler: @escaping @Sendable (Result<Data, Error>) -> Void) {
public func data(handler: @escaping @Sendable (Result<Data, any Error>) -> Void) {
pickerResult.itemProvider.loadDataRepresentation(forTypeIdentifier: contentType.identifier) { data, error in
if let error {
handler(.failure(PHPickerResultImageDataProviderError.pickerProviderError(error)))
Expand Down
8 changes: 4 additions & 4 deletions Sources/General/ImageSource/Source.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ public enum Source: Sendable {

/// The target image should be fetched from the network remotely. The associated `Resource`
/// value defines detailed information such as the image URL and cache key.
case network(Resource)
case network(any Resource)

/// The target image should be provided in a data format, typically as an image
/// from local storage or in any other encoding format, such as Base64.
case provider(ImageDataProvider)
case provider(any ImageDataProvider)

// MARK: Getting Properties

Expand Down Expand Up @@ -112,7 +112,7 @@ extension Source: Hashable {
}

extension Source {
var asResource: Resource? {
var asResource: (any Resource)? {
guard case .network(let resource) = self else {
return nil
}
Expand Down
10 changes: 5 additions & 5 deletions Sources/General/KF.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public enum KF {
/// - Parameter resource: The ``Resource`` object defines data information like key or URL.
/// - Returns: A ``Builder`` for future configuration. After configuring the builder, call its
/// `Builder/set(to:)` to start the image loading.
public static func resource(_ resource: Resource?) -> KF.Builder {
public static func resource(_ resource: (any Resource)?) -> KF.Builder {
source(resource?.convertToSource())
}

Expand All @@ -80,7 +80,7 @@ public enum KF {
/// - Parameter provider: The ``ImageDataProvider`` object contains information about the data.
/// - Returns: A ``Builder`` for future configuration. After configuring the builder, call its
/// `Builder/set(to:)` to start the image loading.
public static func dataProvider(_ provider: ImageDataProvider?) -> KF.Builder {
public static func dataProvider(_ provider: (any ImageDataProvider)?) -> KF.Builder {
source(provider?.convertToSource())
}

Expand Down Expand Up @@ -116,8 +116,8 @@ extension KF {
set { propertyQueue.sync { _placeholder = newValue } }
}
#else
private var _placeholder: Placeholder?
private var placeholder: Placeholder? {
private var _placeholder: (any Placeholder)?
private var placeholder: (any Placeholder)? {
get { propertyQueue.sync { _placeholder } }
set { propertyQueue.sync { _placeholder = newValue } }
}
Expand Down Expand Up @@ -337,7 +337,7 @@ extension KF.Builder {
/// Sets a placeholder which is used while retrieving the image.
/// - Parameter placeholder: A placeholder to show while retrieving the image from its source.
/// - Returns: A ``KF/Builder`` with changes applied.
public func placeholder(_ placeholder: Placeholder?) -> Self {
public func placeholder(_ placeholder: (any Placeholder)?) -> Self {
self.placeholder = placeholder
return self
}
Expand Down
Loading