Skip to content

Commit

Permalink
Merge pull request #26 from roanutil/feature/add-inlinable-notations-…
Browse files Browse the repository at this point in the history
…to-encourage-generic-specialization

Add inlinable notations to public API
  • Loading branch information
roanutil authored Apr 10, 2024
2 parents 7a37be3 + 467294e commit 305ea11
Show file tree
Hide file tree
Showing 25 changed files with 103 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Sources/CoreDataRepository/CoreDataBatchError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ public struct CoreDataBatchError<T>: Error {
/// The underlying error.
public let error: CoreDataError

@inlinable
public var localizedDescription: String {
error.localizedDescription
}

@inlinable
public init(item: T, error: CoreDataError) {
self.item = item
self.error = error
Expand Down
2 changes: 2 additions & 0 deletions Sources/CoreDataRepository/CoreDataError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public enum CoreDataError: Error, Hashable, Sendable {
extension CoreDataError: CustomNSError {
public static let errorDomain: String = "CoreDataRepository"

@inlinable
public var errorCode: Int {
switch self {
case .failedToGetObjectIdFromUrl:
Expand All @@ -133,6 +134,7 @@ extension CoreDataError: CustomNSError {

public static let urlUserInfoKey: String = "ObjectIdUrl"

@inlinable
public var errorUserInfo: [String: Any] {
switch self {
case let .failedToGetObjectIdFromUrl(url):
Expand Down
19 changes: 18 additions & 1 deletion Sources/CoreDataRepository/CoreDataRepository+Aggregate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ extension CoreDataRepository {
// MARK: Count

/// Get the count or quantity of managed object instances that satisfy the predicate.
@inlinable
public func count<Value: Numeric>(
predicate: NSPredicate,
entityDesc: NSEntityDescription,
Expand All @@ -35,6 +36,7 @@ extension CoreDataRepository {
}

/// Subscribe to the count or quantity of managed object instances that satisfy the predicate.
@inlinable
public func countSubscription<Value: Numeric>(
predicate: NSPredicate,
entityDesc: NSEntityDescription,
Expand All @@ -55,6 +57,7 @@ extension CoreDataRepository {
}

/// Subscribe to the count or quantity of managed object instances that satisfy the predicate.
@inlinable
public func countThrowingSubscription<Value: Numeric>(
predicate: NSPredicate,
entityDesc: NSEntityDescription,
Expand All @@ -77,6 +80,7 @@ extension CoreDataRepository {
// MARK: Sum

/// Get the sum of a managed object's numeric property for all instances that satisfy the predicate.
@inlinable
public func sum<Value: Numeric>(
predicate: NSPredicate,
entityDesc: NSEntityDescription,
Expand All @@ -95,6 +99,7 @@ extension CoreDataRepository {
}

/// Subscribe to the sum of a managed object's numeric property for all instances that satisfy the predicate.
@inlinable
public func sumSubscription<Value: Numeric>(
predicate: NSPredicate,
entityDesc: NSEntityDescription,
Expand All @@ -120,6 +125,7 @@ extension CoreDataRepository {
}

/// Subscribe to the sum of a managed object's numeric property for all instances that satisfy the predicate.
@inlinable
public func sumThrowingSubscription<Value: Numeric>(
predicate: NSPredicate,
entityDesc: NSEntityDescription,
Expand Down Expand Up @@ -147,6 +153,7 @@ extension CoreDataRepository {
// MARK: Average

/// Get the average of a managed object's numeric property for all instances that satisfy the predicate.
@inlinable
public func average<Value: Numeric>(
predicate: NSPredicate,
entityDesc: NSEntityDescription,
Expand All @@ -165,6 +172,7 @@ extension CoreDataRepository {
}

/// Subscribe to the average of a managed object's numeric property for all instances that satisfy the predicate.
@inlinable
public func averageSubscription<Value: Numeric>(
predicate: NSPredicate,
entityDesc: NSEntityDescription,
Expand All @@ -190,6 +198,7 @@ extension CoreDataRepository {
}

/// Subscribe to the average of a managed object's numeric property for all instances that satisfy the predicate.
@inlinable
public func averageThrowingSubscription<Value: Numeric>(
predicate: NSPredicate,
entityDesc: NSEntityDescription,
Expand Down Expand Up @@ -217,6 +226,7 @@ extension CoreDataRepository {
// MARK: Min

/// Get the min or minimum of a managed object's numeric property for all instances that satisfy the predicate.
@inlinable
public func min<Value: Numeric>(
predicate: NSPredicate,
entityDesc: NSEntityDescription,
Expand All @@ -236,6 +246,7 @@ extension CoreDataRepository {

/// Subscribe to the min or minimum of a managed object's numeric property for all instances that satisfy the
/// predicate.
@inlinable
public func minSubscription<Value: Numeric>(
predicate: NSPredicate,
entityDesc: NSEntityDescription,
Expand All @@ -262,6 +273,7 @@ extension CoreDataRepository {

/// Subscribe to the min or minimum of a managed object's numeric property for all instances that satisfy the
/// predicate.
@inlinable
public func minThrowingSubscription<Value: Numeric>(
predicate: NSPredicate,
entityDesc: NSEntityDescription,
Expand Down Expand Up @@ -289,6 +301,7 @@ extension CoreDataRepository {
// MARK: Max

/// Get the max or maximum of a managed object's numeric property for all instances that satisfy the predicate.
@inlinable
public func max<Value: Numeric>(
predicate: NSPredicate,
entityDesc: NSEntityDescription,
Expand All @@ -308,6 +321,7 @@ extension CoreDataRepository {

/// Subscribe to the max or maximum of a managed object's numeric property for all instances that satisfy the
/// predicate.
@inlinable
public func maxSubscription<Value: Numeric>(
predicate: NSPredicate,
entityDesc: NSEntityDescription,
Expand All @@ -334,6 +348,7 @@ extension CoreDataRepository {

/// Subscribe to the max or maximum of a managed object's numeric property for all instances that satisfy the
/// predicate.
@inlinable
public func maxThrowingSubscription<Value: Numeric>(
predicate: NSPredicate,
entityDesc: NSEntityDescription,
Expand All @@ -360,6 +375,7 @@ extension CoreDataRepository {

// MARK: Internals

@usableFromInline
enum AggregateFunction: String {
case count
case sum
Expand All @@ -379,7 +395,8 @@ extension CoreDataRepository {
return value
}

private static func send<Value>(
@usableFromInline
static func send<Value>(
function: AggregateFunction,
context: NSManagedObjectContext,
predicate: NSPredicate,
Expand Down
11 changes: 11 additions & 0 deletions Sources/CoreDataRepository/CoreDataRepository+Batch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Foundation

extension CoreDataRepository {
/// Execute a NSBatchInsertRequest against the store.
@inlinable
public func insert(
_ request: NSBatchInsertRequest,
transactionAuthor: String? = nil
Expand All @@ -29,6 +30,7 @@ extension CoreDataRepository {
/// Create a batch of unmanaged models.
///
/// This operation is non-atomic. Each instance may succeed or fail individually.
@inlinable
public func create<Model: UnmanagedModel>(
_ items: [Model],
transactionAuthor: String? = nil
Expand Down Expand Up @@ -68,6 +70,7 @@ extension CoreDataRepository {
}

/// Create a batch of unmanaged models.
@inlinable
public func createAtomically<Model: UnmanagedModel>(
_ items: [Model],
transactionAuthor: String? = nil
Expand All @@ -92,6 +95,7 @@ extension CoreDataRepository {
/// Read a batch of unmanaged models.
///
/// This operation is non-atomic. Each instance may succeed or fail individually.
@inlinable
public func read<Model: UnmanagedModel>(
urls: [URL],
as _: Model.Type
Expand Down Expand Up @@ -130,6 +134,7 @@ extension CoreDataRepository {
}

/// Read a batch of unmanaged models.
@inlinable
public func readAtomically<Model: UnmanagedModel>(
urls: [URL],
as _: Model.Type
Expand All @@ -145,6 +150,7 @@ extension CoreDataRepository {
}

/// Execute a NSBatchUpdateRequest against the store.
@inlinable
public func update(
_ request: NSBatchUpdateRequest,
transactionAuthor: String? = nil
Expand All @@ -163,6 +169,7 @@ extension CoreDataRepository {
/// Update the store with a batch of unmanaged models.
///
/// This operation is non-atomic. Each instance may succeed or fail individually.
@inlinable
public func update<Model: UnmanagedModel>(
_ items: [Model],
transactionAuthor: String? = nil
Expand Down Expand Up @@ -205,6 +212,7 @@ extension CoreDataRepository {
}

/// Update the store with a batch of unmanaged models.
@inlinable
public func updateAtomically<Model: UnmanagedModel>(
_ items: [Model],
transactionAuthor: String? = nil
Expand Down Expand Up @@ -232,6 +240,7 @@ extension CoreDataRepository {
}

/// Execute a NSBatchDeleteRequest against the store.
@inlinable
public func delete(
_ request: NSBatchDeleteRequest,
transactionAuthor: String? = nil
Expand All @@ -250,6 +259,7 @@ extension CoreDataRepository {
/// Delete from the store with a batch of unmanaged models.
///
/// This operation is non-atomic. Each instance may succeed or fail individually.
@inlinable
public func delete(
urls: [URL],
transactionAuthor: String? = nil
Expand Down Expand Up @@ -289,6 +299,7 @@ extension CoreDataRepository {
}

/// Delete from the store with a batch of unmanaged models.
@inlinable
public func deleteAtomically(
urls: [URL],
transactionAuthor: String? = nil
Expand Down
6 changes: 6 additions & 0 deletions Sources/CoreDataRepository/CoreDataRepository+CRUD.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Foundation

extension CoreDataRepository {
/// Create an instance in the store.
@inlinable
public func create<Model: UnmanagedModel>(
_ item: Model,
transactionAuthor: String? = nil
Expand All @@ -37,6 +38,7 @@ extension CoreDataRepository {
}

/// Read an instance from the store.
@inlinable
public func read<Model: UnmanagedReadOnlyModel>(
_ url: URL,
of _: Model.Type
Expand All @@ -50,6 +52,7 @@ extension CoreDataRepository {
}

/// Update the store with an unmanaged model.
@inlinable
public func update<Model: UnmanagedModel>(
_ url: URL,
with item: Model,
Expand All @@ -72,6 +75,7 @@ extension CoreDataRepository {
}

/// Delete an instance from the store.
@inlinable
public func delete(
_ url: URL,
transactionAuthor: String? = nil
Expand All @@ -93,6 +97,7 @@ extension CoreDataRepository {
}

/// Subscribe to updates of an instance in the store.
@inlinable
public func readSubscription<Model: UnmanagedReadOnlyModel>(_ url: URL, of _: Model.Type)
-> AsyncStream<Result<Model, CoreDataError>>
{
Expand Down Expand Up @@ -120,6 +125,7 @@ extension CoreDataRepository {
}

/// Subscribe to updates of an instance in the store.
@inlinable
public func readThrowingSubscription<Model: UnmanagedReadOnlyModel>(_ url: URL, of _: Model.Type)
-> AsyncThrowingStream<Model, Error>
{
Expand Down
1 change: 1 addition & 0 deletions Sources/CoreDataRepository/CoreDataRepository+Custom.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ extension CoreDataRepository {
///
/// The caller is responsible for saving the contexts and cleaning up if needed.
/// All this method provides is the contexts and mapping `Error` into ``CoreDataError``.
@inlinable
public func custom<T>(
schedule: NSManagedObjectContext.ScheduledTaskType = .enqueued,
block: @escaping (
Expand Down
4 changes: 4 additions & 0 deletions Sources/CoreDataRepository/CoreDataRepository+Fetch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Foundation

extension CoreDataRepository {
/// Fetch items from the store with a ``NSFetchRequest``.
@inlinable
public func fetch<Model: UnmanagedReadOnlyModel>(
_ request: NSFetchRequest<Model.ManagedModel>,
as _: Model.Type
Expand All @@ -21,6 +22,7 @@ extension CoreDataRepository {
}

/// Fetch items from the store with a ``NSFetchRequest`` and receive updates as the store changes.
@inlinable
public func fetchSubscription<Model: UnmanagedReadOnlyModel>(
_ request: NSFetchRequest<Model.ManagedModel>,
of _: Model.Type
Expand All @@ -39,6 +41,7 @@ extension CoreDataRepository {
}

/// Fetch items from the store with a ``NSFetchRequest`` and receive updates as the store changes.
@inlinable
public func fetchThrowingSubscription<Model: UnmanagedReadOnlyModel>(
_ request: NSFetchRequest<Model.ManagedModel>,
of _: Model.Type
Expand All @@ -57,6 +60,7 @@ extension CoreDataRepository {
}

/// Fetch items from the store with a ``NSFetchRequest`` and transform the results.
@inlinable
public func fetch<Managed, Output>(
request: NSFetchRequest<Managed>,
operation: @escaping (_ results: [Managed]) throws -> Output
Expand Down
1 change: 1 addition & 0 deletions Sources/CoreDataRepository/CoreDataRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public final class CoreDataRepository {
/// to be performed in.
public let context: NSManagedObjectContext

@inlinable
public init(context: NSManagedObjectContext) {
self.context = context
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import CoreData
import Foundation

/// Subscription provider that sends updates when an aggregate fetch request changes
@usableFromInline
final class AggregateSubscription<Value>: Subscription<Value, NSDictionary, NSManagedObject> where Value: Numeric {
@usableFromInline
override func fetch() {
frc.managedObjectContext.perform { [weak self, frc, request] in
guard frc.fetchedObjects != nil else {
Expand All @@ -37,6 +39,7 @@ final class AggregateSubscription<Value>: Subscription<Value, NSDictionary, NSMa
}
}

@usableFromInline
convenience init(
function: CoreDataRepository.AggregateFunction,
context: NSManagedObjectContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import CoreData
import Foundation

/// Subscription provider that sends updates when an aggregate fetch request changes
@usableFromInline
final class AggregateThrowingSubscription<Value>: ThrowingSubscription<Value, NSDictionary, NSManagedObject>
where Value: Numeric
{
@usableFromInline
override func fetch() {
frc.managedObjectContext.perform { [weak self, frc, request] in
guard frc.fetchedObjects != nil else {
Expand All @@ -39,6 +41,7 @@ final class AggregateThrowingSubscription<Value>: ThrowingSubscription<Value, NS
}
}

@usableFromInline
convenience init(
function: CoreDataRepository.AggregateFunction,
context: NSManagedObjectContext,
Expand Down
Loading

0 comments on commit 305ea11

Please sign in to comment.