Skip to content

Commit

Permalink
refactor: adapt for Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
Asura19 committed Oct 12, 2024
1 parent b753ca9 commit 952ab5b
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 34 deletions.
12 changes: 12 additions & 0 deletions ReerKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,11 @@
D094C96229F4606600491CD8 /* UIDeviceExtensionsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D094C96129F4606600491CD8 /* UIDeviceExtensionsTests.swift */; };
D094C96429F4631200491CD8 /* Memory.swift in Sources */ = {isa = PBXBuildFile; fileRef = D094C96329F4631200491CD8 /* Memory.swift */; };
D094C96629F46A0C00491CD8 /* MemoryTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D094C96529F46A0C00491CD8 /* MemoryTests.swift */; };
D0991CB22CBA56C100C6BE5A /* MutexLock.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0991CB12CBA56C100C6BE5A /* MutexLock.swift */; };
D0991CB32CBA56C100C6BE5A /* MutexLock.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0991CB12CBA56C100C6BE5A /* MutexLock.swift */; };
D0991CB42CBA56C100C6BE5A /* MutexLock.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0991CB12CBA56C100C6BE5A /* MutexLock.swift */; };
D0991CB52CBA56C100C6BE5A /* MutexLock.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0991CB12CBA56C100C6BE5A /* MutexLock.swift */; };
D0991CB62CBA56C100C6BE5A /* MutexLock.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0991CB12CBA56C100C6BE5A /* MutexLock.swift */; };
D09D724029DC94BF00636935 /* big_buck_bunny_720p_1mb.mp4 in Resources */ = {isa = PBXBuildFile; fileRef = D09D723F29DC94BF00636935 /* big_buck_bunny_720p_1mb.mp4 */; };
D0A0040628620D9900D2FA7C /* TypeNameDescribableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0A0040528620D9900D2FA7C /* TypeNameDescribableTests.swift */; };
D0A6B3A32924AD11008B7FDA /* UIResponder+REExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0A6B3A22924AD11008B7FDA /* UIResponder+REExtensions.swift */; };
Expand Down Expand Up @@ -992,6 +997,7 @@
D094C96129F4606600491CD8 /* UIDeviceExtensionsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIDeviceExtensionsTests.swift; sourceTree = "<group>"; };
D094C96329F4631200491CD8 /* Memory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Memory.swift; sourceTree = "<group>"; };
D094C96529F46A0C00491CD8 /* MemoryTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MemoryTests.swift; sourceTree = "<group>"; };
D0991CB12CBA56C100C6BE5A /* MutexLock.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MutexLock.swift; sourceTree = "<group>"; };
D09D723F29DC94BF00636935 /* big_buck_bunny_720p_1mb.mp4 */ = {isa = PBXFileReference; lastKnownFileType = file; path = big_buck_bunny_720p_1mb.mp4; sourceTree = "<group>"; };
D0A0040528620D9900D2FA7C /* TypeNameDescribableTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TypeNameDescribableTests.swift; sourceTree = "<group>"; };
D0A6B3A22924AD11008B7FDA /* UIResponder+REExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIResponder+REExtensions.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1312,6 +1318,7 @@
D0038B7E2856581600A890F9 /* UnfairLock.swift */,
D0038B8028565B9400A890F9 /* ReadWriteLock.swift */,
D004CC48285A41F500C41360 /* Synchronizing.swift */,
D0991CB12CBA56C100C6BE5A /* MutexLock.swift */,
);
path = Lock;
sourceTree = "<group>";
Expand Down Expand Up @@ -2160,6 +2167,7 @@
D01D6DCE2A06CBE000AEDA3D /* Vibrator.swift in Sources */,
D0D311392AB8522F00E54361 /* Rounded.swift in Sources */,
D04B85DC29F9A50600177357 /* Throttler.swift in Sources */,
D0991CB32CBA56C100C6BE5A /* MutexLock.swift in Sources */,
D04B861029F9A59600177357 /* UIViewController+REExtensions.swift in Sources */,
D04B85DD29F9A50600177357 /* Debouncer.swift in Sources */,
D04B861929F9A59600177357 /* UITableView+REExtensions.swift in Sources */,
Expand Down Expand Up @@ -2307,6 +2315,7 @@
D01D6DCF2A06CBE000AEDA3D /* Vibrator.swift in Sources */,
D0D3113A2AB8522F00E54361 /* Rounded.swift in Sources */,
D04B86F929F9A71100177357 /* URLRequest+REExtensions.swift in Sources */,
D0991CB42CBA56C100C6BE5A /* MutexLock.swift in Sources */,
D04B868029F9A6D900177357 /* Color+REExtensions.swift in Sources */,
D04B86BC29F9A6FC00177357 /* WeakProxy.swift in Sources */,
D04B872929F9A71900177357 /* UIResponder+REExtensions.swift in Sources */,
Expand Down Expand Up @@ -2453,6 +2462,7 @@
D01D6DD02A06CBE000AEDA3D /* Vibrator.swift in Sources */,
D0D3113B2AB8522F00E54361 /* Rounded.swift in Sources */,
D04B86EB29F9A71000177357 /* URLRequest+REExtensions.swift in Sources */,
D0991CB52CBA56C100C6BE5A /* MutexLock.swift in Sources */,
D04B867D29F9A6D800177357 /* Color+REExtensions.swift in Sources */,
D04B86C029F9A6FD00177357 /* WeakProxy.swift in Sources */,
D04B870829F9A71900177357 /* UIResponder+REExtensions.swift in Sources */,
Expand Down Expand Up @@ -2504,6 +2514,7 @@
D0185A7A2C7F501900100E3E /* NanoID.swift in Sources */,
D0185A7B2C7F501900100E3E /* Reachability.swift in Sources */,
D0185A7C2C7F501900100E3E /* GlobalFunctions.swift in Sources */,
D0991CB62CBA56C100C6BE5A /* MutexLock.swift in Sources */,
D0185A7D2C7F501900100E3E /* RETimer.swift in Sources */,
D0185A7E2C7F501900100E3E /* Invocation.swift in Sources */,
D0185A7F2C7F501900100E3E /* CountdownTimer.swift in Sources */,
Expand Down Expand Up @@ -2766,6 +2777,7 @@
D0D311242AB4458F00E54361 /* CALayer+REExtensions.swift in Sources */,
D0B1211E2855156200B5B326 /* TypeNameDescribable.swift in Sources */,
D07E53AF2883137000921873 /* WeakProxy.swift in Sources */,
D0991CB22CBA56C100C6BE5A /* MutexLock.swift in Sources */,
D0122B0B29EEFEB8008311CF /* UICollectionView+REExtensions.swift in Sources */,
D004CC49285A41F500C41360 /* Synchronizing.swift in Sources */,
D01DABC229B4FAA700ABD25E /* Throttler.swift in Sources */,
Expand Down
3 changes: 2 additions & 1 deletion Sources/Foundation/Data+REExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public extension ReerForEquatable where Base == Data {
throw JSONError.invalidArray
}
}

#if !os(Linux)
/// ReerKit: Generate a random length of Data.
static func random(ofLength length: Int) -> Data {
var randomData = Data(count: length)
Expand All @@ -164,6 +164,7 @@ public extension ReerForEquatable where Base == Data {
return Data((0..<length).map { _ in UInt8.random(in: UInt8.min ... UInt8.max) })
}
}
#endif
}

#if canImport(CommonCrypto)
Expand Down
8 changes: 4 additions & 4 deletions Sources/Foundation/NSAttributedString+REExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ public extension Reer where Base: NSAttributedString {
return base.attributes(at: 0, effectiveRange: nil)
}

#if !os(Linux)
@available(*, deprecated, renamed: "numberOfLines(forWidth:ignoreBlankLines:)", message: "Use numberOfLines(forWidth:ignoringBlankLines:) instead.")
func lines(forWidth width: CGFloat, ignoreBlankLines: Bool = false) -> Int {
return numberOfLines(forWidth: width, ignoreBlankLines: ignoreBlankLines)
}

#if !os(Linux)
/// ReerKit: Calculate lines for a `NSAttributedString` with a width constrained.
/// - Parameters:
/// - width: A constrained of container view width.
Expand Down Expand Up @@ -77,7 +77,6 @@ public extension Reer where Base: NSAttributedString {

return numberOfLines
}
#endif

/// ReerKit: Calculate height for a `NSAttributedString` with a constrained width.
/// - Parameter width: A constrained of container view width.
Expand All @@ -87,13 +86,14 @@ public extension Reer where Base: NSAttributedString {
let rect = base.boundingRect(with: maxSize, options: [.usesLineFragmentOrigin, .usesFontLeading], context: nil)
return rect.height
}
#endif

/// ReerKit: Applies given attributes to the new instance of NSAttributedString initialized with self object.
///
/// - Parameter attributes: Dictionary of attributes.
/// - Returns: NSAttributedString with applied attributes.
func with(attributes: [NSAttributedString.Key: Any]) -> NSMutableAttributedString {
guard !base.string.isEmpty else { return NSMutableAttributedString() }
guard !base.string.isEmpty else { return NSMutableAttributedString(string: "") }

let copy = NSMutableAttributedString(attributedString: base)
copy.addAttributes(attributes, range: NSRange(0..<base.length))
Expand All @@ -112,7 +112,7 @@ public extension Reer where Base: NSAttributedString {
toRangesMatching pattern: String,
options: NSRegularExpression.Options = []
) -> NSMutableAttributedString {
guard let pattern = try? NSRegularExpression(pattern: pattern, options: options) else { return NSMutableAttributedString() }
guard let pattern = try? NSRegularExpression(pattern: pattern, options: options) else { return NSMutableAttributedString(string: "") }

let matches = pattern.matches(in: base.string, options: [], range: NSRange(0..<base.length))
let result = NSMutableAttributedString(attributedString: base)
Expand Down
2 changes: 1 addition & 1 deletion Sources/Foundation/NSPredicate+REExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#if canImport(Foundation)
#if canImport(Foundation) && !os(Linux)
import Foundation

public extension Reer where Base: NSPredicate {
Expand Down
25 changes: 0 additions & 25 deletions Sources/Foundation/NSRegularExpression+REExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,30 +38,6 @@ public extension Reer where Base: NSRegularExpression {
/// The current state of the matching progress. See `NSRegularExpression.MatchingFlags` for the possible values.
/// - stop:
/// A reference to a Boolean value. The Block can set the value to true to stop further processing of the array. The stop argument is an out-only argument. You should only ever set this Boolean to true within the Block.
#if os(Linux)
func enumerateMatches(
in string: String,
options: NSRegularExpression.MatchingOptions = [],
range: Range<String.Index>,
using block: @escaping (
_ result: NSTextCheckingResult?,
_ flags: MatchingFlags,
_ stop: inout Bool
) -> Void
) {
base.enumerateMatches(
in: string,
options: options,
range: NSRange(range, in: string)
) { result, flags, stop in
var shouldStop = false
block(result, flags, &shouldStop)
if shouldStop {
stop.pointee = true
}
}
}
#else
func enumerateMatches(
in string: String,
options: NSRegularExpression.MatchingOptions = [],
Expand All @@ -80,7 +56,6 @@ public extension Reer where Base: NSRegularExpression {
}
}
}
#endif

/// ReerKit: Returns an array containing all the matches of the regular expression in the string.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@

/// ReerKit: Combinations of AnyObject extension protocols.
/// You must conform this protocol by yourself if your class is NOT inheriting from `NSObject`.

#if canImport(ObjectiveC)
public typealias AnyObjectExtensionable =
Associatable
& OnceExecutable
& Swizzlable
& DeinitObservable
#endif
4 changes: 4 additions & 0 deletions Sources/Utility/Debouncer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ import Foundation
public final class Debouncer {
private let queue: DispatchQueue
public private(set) var workItem = DispatchWorkItem(block: {})
#if os(Linux)
private let lock = MutexLock()
#else
private let lock = UnfairLock()
#endif

public init(queue: DispatchQueue = .main) {
self.queue = queue
Expand Down
2 changes: 1 addition & 1 deletion Sources/Utility/GlobalFunctions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#if canImport(Dispatch)
#if canImport(Dispatch) && !os(Linux)
import Dispatch

/// ReerKit: Get current queue label.
Expand Down
2 changes: 1 addition & 1 deletion Sources/Utility/Lock/UnfairLock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#if canImport(os)
#if canImport(os) && !os(Linux)
import os.lock

/// ReerKit: A wrapper of `os_unfair_lock`
Expand Down
4 changes: 4 additions & 0 deletions Sources/Utility/Throttler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ public final class Throttler {
private var isPending = false
private var hasExecuted = false
private var latestAction: () -> Void = {}
#if os(Linux)
private let lock = MutexLock()
#else
private let lock = UnfairLock()
#endif

private let queue: DispatchQueue
private let performMode: PerformMode
Expand Down
2 changes: 1 addition & 1 deletion Sources/Utility/Weak/WeakProxy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#if canImport(Foundation)
#if canImport(Foundation) && !os(Linux)
import Foundation

/// A proxy used to hold a weak object.
Expand Down

0 comments on commit 952ab5b

Please sign in to comment.