Skip to content

Commit

Permalink
Move copy function into collection class
Browse files Browse the repository at this point in the history
  • Loading branch information
lukellmann committed Jul 28, 2023
1 parent 919a5e5 commit 6c29dc3
Show file tree
Hide file tree
Showing 13 changed files with 61 additions and 71 deletions.
14 changes: 7 additions & 7 deletions common/api/common.api
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,6 @@ public final class dev/kord/common/entity/ActivityFlagKt {
public static final fun ActivityFlags ([Ldev/kord/common/entity/ActivityFlag;)Ldev/kord/common/entity/ActivityFlags;
public static final fun ActivityFlags ([Ldev/kord/common/entity/ActivityFlags;)Ldev/kord/common/entity/ActivityFlags;
public static final fun ActivityFlags0 (Ljava/lang/Iterable;)Ldev/kord/common/entity/ActivityFlags;
public static final fun copy (Ldev/kord/common/entity/ActivityFlags;Lkotlin/jvm/functions/Function1;)Ldev/kord/common/entity/ActivityFlags;
}

public final class dev/kord/common/entity/ActivityFlags {
Expand All @@ -270,6 +269,7 @@ public final class dev/kord/common/entity/ActivityFlags {
public synthetic fun <init> (IILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun contains (Ldev/kord/common/entity/ActivityFlag;)Z
public final fun contains (Ldev/kord/common/entity/ActivityFlags;)Z
public final fun copy (Lkotlin/jvm/functions/Function1;)Ldev/kord/common/entity/ActivityFlags;
public fun equals (Ljava/lang/Object;)Z
public final fun getCode ()I
public final fun getValues ()Ljava/util/Set;
Expand Down Expand Up @@ -700,7 +700,6 @@ public final class dev/kord/common/entity/ApplicationFlagKt {
public static final fun ApplicationFlags ([Ldev/kord/common/entity/ApplicationFlag;)Ldev/kord/common/entity/ApplicationFlags;
public static final fun ApplicationFlags ([Ldev/kord/common/entity/ApplicationFlags;)Ldev/kord/common/entity/ApplicationFlags;
public static final fun ApplicationFlags0 (Ljava/lang/Iterable;)Ldev/kord/common/entity/ApplicationFlags;
public static final fun copy (Ldev/kord/common/entity/ApplicationFlags;Lkotlin/jvm/functions/Function1;)Ldev/kord/common/entity/ApplicationFlags;
}

public final class dev/kord/common/entity/ApplicationFlags {
Expand All @@ -712,6 +711,7 @@ public final class dev/kord/common/entity/ApplicationFlags {
public final fun contains (Ldev/kord/common/entity/ApplicationFlag;)Z
public final fun contains (Ldev/kord/common/entity/ApplicationFlags;)Z
public final fun copy (I)Ldev/kord/common/entity/ApplicationFlags;
public final fun copy (Lkotlin/jvm/functions/Function1;)Ldev/kord/common/entity/ApplicationFlags;
public static synthetic fun copy$default (Ldev/kord/common/entity/ApplicationFlags;IILjava/lang/Object;)Ldev/kord/common/entity/ApplicationFlags;
public fun equals (Ljava/lang/Object;)Z
public final fun getCode ()I
Expand Down Expand Up @@ -6765,7 +6765,6 @@ public final class dev/kord/common/entity/GuildMemberFlagKt {
public static final fun GuildMemberFlags ([Ldev/kord/common/entity/GuildMemberFlag;)Ldev/kord/common/entity/GuildMemberFlags;
public static final fun GuildMemberFlags ([Ldev/kord/common/entity/GuildMemberFlags;)Ldev/kord/common/entity/GuildMemberFlags;
public static final fun GuildMemberFlags0 (Ljava/lang/Iterable;)Ldev/kord/common/entity/GuildMemberFlags;
public static final fun copy (Ldev/kord/common/entity/GuildMemberFlags;Lkotlin/jvm/functions/Function1;)Ldev/kord/common/entity/GuildMemberFlags;
}

public final class dev/kord/common/entity/GuildMemberFlags {
Expand All @@ -6777,6 +6776,7 @@ public final class dev/kord/common/entity/GuildMemberFlags {
public final fun contains (Ldev/kord/common/entity/GuildMemberFlag;)Z
public final fun contains (Ldev/kord/common/entity/GuildMemberFlags;)Z
public final fun copy (I)Ldev/kord/common/entity/GuildMemberFlags;
public final fun copy (Lkotlin/jvm/functions/Function1;)Ldev/kord/common/entity/GuildMemberFlags;
public static synthetic fun copy$default (Ldev/kord/common/entity/GuildMemberFlags;IILjava/lang/Object;)Ldev/kord/common/entity/GuildMemberFlags;
public fun equals (Ljava/lang/Object;)Z
public final fun getCode ()I
Expand Down Expand Up @@ -7364,7 +7364,6 @@ public final class dev/kord/common/entity/MessageFlagKt {
public static final fun MessageFlags ([Ldev/kord/common/entity/MessageFlag;)Ldev/kord/common/entity/MessageFlags;
public static final fun MessageFlags ([Ldev/kord/common/entity/MessageFlags;)Ldev/kord/common/entity/MessageFlags;
public static final fun MessageFlags0 (Ljava/lang/Iterable;)Ldev/kord/common/entity/MessageFlags;
public static final fun copy (Ldev/kord/common/entity/MessageFlags;Lkotlin/jvm/functions/Function1;)Ldev/kord/common/entity/MessageFlags;
}

public final class dev/kord/common/entity/MessageFlags {
Expand All @@ -7376,6 +7375,7 @@ public final class dev/kord/common/entity/MessageFlags {
public final fun contains (Ldev/kord/common/entity/MessageFlag;)Z
public final fun contains (Ldev/kord/common/entity/MessageFlags;)Z
public final fun copy (I)Ldev/kord/common/entity/MessageFlags;
public final fun copy (Lkotlin/jvm/functions/Function1;)Ldev/kord/common/entity/MessageFlags;
public static synthetic fun copy$default (Ldev/kord/common/entity/MessageFlags;IILjava/lang/Object;)Ldev/kord/common/entity/MessageFlags;
public fun equals (Ljava/lang/Object;)Z
public final fun getCode ()I
Expand Down Expand Up @@ -8009,7 +8009,6 @@ public final class dev/kord/common/entity/PermissionKt {
public static final fun Permissions ([Ldev/kord/common/entity/Permission;)Ldev/kord/common/entity/Permissions;
public static final fun Permissions ([Ldev/kord/common/entity/Permissions;)Ldev/kord/common/entity/Permissions;
public static final fun Permissions0 (Ljava/lang/Iterable;)Ldev/kord/common/entity/Permissions;
public static final fun copy (Ldev/kord/common/entity/Permissions;Lkotlin/jvm/functions/Function1;)Ldev/kord/common/entity/Permissions;
}

public final class dev/kord/common/entity/Permissions {
Expand All @@ -8021,6 +8020,7 @@ public final class dev/kord/common/entity/Permissions {
public final fun contains (Ldev/kord/common/entity/Permission;)Z
public final fun contains (Ldev/kord/common/entity/Permissions;)Z
public final fun copy (Ldev/kord/common/DiscordBitSet;)Ldev/kord/common/entity/Permissions;
public final fun copy (Lkotlin/jvm/functions/Function1;)Ldev/kord/common/entity/Permissions;
public static synthetic fun copy$default (Ldev/kord/common/entity/Permissions;Ldev/kord/common/DiscordBitSet;ILjava/lang/Object;)Ldev/kord/common/entity/Permissions;
public fun equals (Ljava/lang/Object;)Z
public final fun getCode ()Ldev/kord/common/DiscordBitSet;
Expand Down Expand Up @@ -8442,7 +8442,6 @@ public final class dev/kord/common/entity/SystemChannelFlagKt {
public static final fun SystemChannelFlags ([Ldev/kord/common/entity/SystemChannelFlag;)Ldev/kord/common/entity/SystemChannelFlags;
public static final fun SystemChannelFlags ([Ldev/kord/common/entity/SystemChannelFlags;)Ldev/kord/common/entity/SystemChannelFlags;
public static final fun SystemChannelFlags0 (Ljava/lang/Iterable;)Ldev/kord/common/entity/SystemChannelFlags;
public static final fun copy (Ldev/kord/common/entity/SystemChannelFlags;Lkotlin/jvm/functions/Function1;)Ldev/kord/common/entity/SystemChannelFlags;
}

public final class dev/kord/common/entity/SystemChannelFlags {
Expand All @@ -8454,6 +8453,7 @@ public final class dev/kord/common/entity/SystemChannelFlags {
public final fun contains (Ldev/kord/common/entity/SystemChannelFlag;)Z
public final fun contains (Ldev/kord/common/entity/SystemChannelFlags;)Z
public final fun copy (I)Ldev/kord/common/entity/SystemChannelFlags;
public final fun copy (Lkotlin/jvm/functions/Function1;)Ldev/kord/common/entity/SystemChannelFlags;
public static synthetic fun copy$default (Ldev/kord/common/entity/SystemChannelFlags;IILjava/lang/Object;)Ldev/kord/common/entity/SystemChannelFlags;
public fun equals (Ljava/lang/Object;)Z
public final fun getCode ()I
Expand Down Expand Up @@ -8645,7 +8645,6 @@ public final class dev/kord/common/entity/UserFlagKt {
public static final fun UserFlags ([Ldev/kord/common/entity/UserFlag;)Ldev/kord/common/entity/UserFlags;
public static final fun UserFlags ([Ldev/kord/common/entity/UserFlags;)Ldev/kord/common/entity/UserFlags;
public static final fun UserFlags0 (Ljava/lang/Iterable;)Ldev/kord/common/entity/UserFlags;
public static final fun copy (Ldev/kord/common/entity/UserFlags;Lkotlin/jvm/functions/Function1;)Ldev/kord/common/entity/UserFlags;
}

public final class dev/kord/common/entity/UserFlags {
Expand All @@ -8657,6 +8656,7 @@ public final class dev/kord/common/entity/UserFlags {
public final fun contains (Ldev/kord/common/entity/UserFlag;)Z
public final fun contains (Ldev/kord/common/entity/UserFlags;)Z
public final fun copy (I)Ldev/kord/common/entity/UserFlags;
public final fun copy (Lkotlin/jvm/functions/Function1;)Ldev/kord/common/entity/UserFlags;
public static synthetic fun copy$default (Ldev/kord/common/entity/UserFlags;IILjava/lang/Object;)Ldev/kord/common/entity/UserFlags;
public fun equals (Ljava/lang/Object;)Z
public final fun getCode ()I
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ public class ActivityFlags(
public operator fun minus(flags: ActivityFlags): ActivityFlags =
ActivityFlags(this.code and flags.code.inv())

public inline fun copy(block: Builder.() -> Unit): ActivityFlags {
contract { callsInPlace(block, EXACTLY_ONCE) }
return Builder(code).apply(block).flags()
}

override fun equals(other: Any?): Boolean = this === other ||
(other is ActivityFlags && this.code == other.code)

Expand Down Expand Up @@ -164,11 +169,6 @@ public fun ActivityFlags(flags: Iterable<ActivityFlag>): ActivityFlags = Activit
public fun ActivityFlags(flags: Iterable<ActivityFlags>): ActivityFlags = ActivityFlags {
flags.forEach { +it } }

public inline fun ActivityFlags.copy(block: ActivityFlags.Builder.() -> Unit): ActivityFlags {
contract { callsInPlace(block, EXACTLY_ONCE) }
return ActivityFlags.Builder(code).apply(block).flags()
}

/**
* See [ActivityFlag]s in the
* [Discord Developer Documentation](https://discord.com/developers/docs/topics/gateway-events#activity-object-activity-flags).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ public class ApplicationFlags(
public operator fun minus(flags: ApplicationFlags): ApplicationFlags =
ApplicationFlags(this.code and flags.code.inv())

public inline fun copy(block: Builder.() -> Unit): ApplicationFlags {
contract { callsInPlace(block, EXACTLY_ONCE) }
return Builder(code).apply(block).flags()
}

override fun equals(other: Any?): Boolean = this === other ||
(other is ApplicationFlags && this.code == other.code)

Expand Down Expand Up @@ -187,12 +192,6 @@ public fun ApplicationFlags(flags: Iterable<ApplicationFlag>): ApplicationFlags
public fun ApplicationFlags(flags: Iterable<ApplicationFlags>): ApplicationFlags =
ApplicationFlags { flags.forEach { +it } }

public inline fun ApplicationFlags.copy(block: ApplicationFlags.Builder.() -> Unit):
ApplicationFlags {
contract { callsInPlace(block, EXACTLY_ONCE) }
return ApplicationFlags.Builder(code).apply(block).flags()
}

/**
* See [ApplicationFlag]s in the
* [Discord Developer Documentation](https://discord.com/developers/docs/resources/application#application-object-application-flags).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ public class GuildMemberFlags(
public operator fun minus(flags: GuildMemberFlags): GuildMemberFlags =
GuildMemberFlags(this.code and flags.code.inv())

public inline fun copy(block: Builder.() -> Unit): GuildMemberFlags {
contract { callsInPlace(block, EXACTLY_ONCE) }
return Builder(code).apply(block).flags()
}

override fun equals(other: Any?): Boolean = this === other ||
(other is GuildMemberFlags && this.code == other.code)

Expand Down Expand Up @@ -186,12 +191,6 @@ public fun GuildMemberFlags(flags: Iterable<GuildMemberFlag>): GuildMemberFlags
public fun GuildMemberFlags(flags: Iterable<GuildMemberFlags>): GuildMemberFlags =
GuildMemberFlags { flags.forEach { +it } }

public inline fun GuildMemberFlags.copy(block: GuildMemberFlags.Builder.() -> Unit):
GuildMemberFlags {
contract { callsInPlace(block, EXACTLY_ONCE) }
return GuildMemberFlags.Builder(code).apply(block).flags()
}

/**
* See [GuildMemberFlag]s in the
* [Discord Developer Documentation](https://discord.com/developers/docs/resources/guild#guild-member-object-guild-member-flags).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ public class MessageFlags(
public operator fun minus(flags: MessageFlags): MessageFlags =
MessageFlags(this.code and flags.code.inv())

public inline fun copy(block: Builder.() -> Unit): MessageFlags {
contract { callsInPlace(block, EXACTLY_ONCE) }
return Builder(code).apply(block).flags()
}

override fun equals(other: Any?): Boolean = this === other ||
(other is MessageFlags && this.code == other.code)

Expand Down Expand Up @@ -180,11 +185,6 @@ public fun MessageFlags(flags: Iterable<MessageFlag>): MessageFlags = MessageFla
public fun MessageFlags(flags: Iterable<MessageFlags>): MessageFlags = MessageFlags {
flags.forEach { +it } }

public inline fun MessageFlags.copy(block: MessageFlags.Builder.() -> Unit): MessageFlags {
contract { callsInPlace(block, EXACTLY_ONCE) }
return MessageFlags.Builder(code).apply(block).flags()
}

/**
* See [MessageFlag]s in the
* [Discord Developer Documentation](https://discord.com/developers/docs/resources/channel#message-object-message-flags).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ public class Permissions(

public operator fun minus(flags: Permissions): Permissions = Permissions(this.code - flags.code)

public inline fun copy(block: Builder.() -> Unit): Permissions {
contract { callsInPlace(block, EXACTLY_ONCE) }
return Builder(code).apply(block).flags()
}

override fun equals(other: Any?): Boolean = this === other ||
(other is Permissions && this.code == other.code)

Expand Down Expand Up @@ -173,11 +178,6 @@ public fun Permissions(flags: Iterable<Permission>): Permissions = Permissions {
public fun Permissions(flags: Iterable<Permissions>): Permissions = Permissions {
flags.forEach { +it } }

public inline fun Permissions.copy(block: Permissions.Builder.() -> Unit): Permissions {
contract { callsInPlace(block, EXACTLY_ONCE) }
return Permissions.Builder(code).apply(block).flags()
}

/**
* See [Permission]s in the
* [Discord Developer Documentation](https://discord.com/developers/docs/topics/permissions).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ public class SystemChannelFlags(
public operator fun minus(flags: SystemChannelFlags): SystemChannelFlags =
SystemChannelFlags(this.code and flags.code.inv())

public inline fun copy(block: Builder.() -> Unit): SystemChannelFlags {
contract { callsInPlace(block, EXACTLY_ONCE) }
return Builder(code).apply(block).flags()
}

override fun equals(other: Any?): Boolean = this === other ||
(other is SystemChannelFlags && this.code == other.code)

Expand Down Expand Up @@ -188,12 +193,6 @@ public fun SystemChannelFlags(flags: Iterable<SystemChannelFlag>): SystemChannel
public fun SystemChannelFlags(flags: Iterable<SystemChannelFlags>): SystemChannelFlags =
SystemChannelFlags { flags.forEach { +it } }

public inline fun SystemChannelFlags.copy(block: SystemChannelFlags.Builder.() -> Unit):
SystemChannelFlags {
contract { callsInPlace(block, EXACTLY_ONCE) }
return SystemChannelFlags.Builder(code).apply(block).flags()
}

/**
* See [SystemChannelFlag]s in the
* [Discord Developer Documentation](https://discord.com/developers/docs/resources/guild#guild-object-system-channel-flags).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ public class UserFlags(
public operator fun minus(flags: UserFlags): UserFlags =
UserFlags(this.code and flags.code.inv())

public inline fun copy(block: Builder.() -> Unit): UserFlags {
contract { callsInPlace(block, EXACTLY_ONCE) }
return Builder(code).apply(block).flags()
}

override fun equals(other: Any?): Boolean = this === other ||
(other is UserFlags && this.code == other.code)

Expand Down Expand Up @@ -172,11 +177,6 @@ public fun UserFlags(flags: Iterable<UserFlag>): UserFlags = UserFlags { flags.f
@JvmName("UserFlags0")
public fun UserFlags(flags: Iterable<UserFlags>): UserFlags = UserFlags { flags.forEach { +it } }

public inline fun UserFlags.copy(block: UserFlags.Builder.() -> Unit): UserFlags {
contract { callsInPlace(block, EXACTLY_ONCE) }
return UserFlags.Builder(code).apply(block).flags()
}

/**
* See [UserFlag]s in the
* [Discord Developer Documentation](https://discord.com/developers/docs/resources/user#user-object-user-flags).
Expand Down
2 changes: 1 addition & 1 deletion gateway/api/gateway.api
Original file line number Diff line number Diff line change
Expand Up @@ -1294,7 +1294,6 @@ public final class dev/kord/gateway/IntentKt {
public static final fun Intents ([Ldev/kord/gateway/Intent;)Ldev/kord/gateway/Intents;
public static final fun Intents ([Ldev/kord/gateway/Intents;)Ldev/kord/gateway/Intents;
public static final fun Intents0 (Ljava/lang/Iterable;)Ldev/kord/gateway/Intents;
public static final fun copy (Ldev/kord/gateway/Intents;Lkotlin/jvm/functions/Function1;)Ldev/kord/gateway/Intents;
}

public final class dev/kord/gateway/IntentUtil {
Expand All @@ -1313,6 +1312,7 @@ public final class dev/kord/gateway/Intents {
public final fun contains (Ldev/kord/gateway/Intent;)Z
public final fun contains (Ldev/kord/gateway/Intents;)Z
public final fun copy (Ldev/kord/common/DiscordBitSet;)Ldev/kord/gateway/Intents;
public final fun copy (Lkotlin/jvm/functions/Function1;)Ldev/kord/gateway/Intents;
public static synthetic fun copy$default (Ldev/kord/gateway/Intents;Ldev/kord/common/DiscordBitSet;ILjava/lang/Object;)Ldev/kord/gateway/Intents;
public fun equals (Ljava/lang/Object;)Z
public final fun getCode ()Ldev/kord/common/DiscordBitSet;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ public class Intents(

public operator fun minus(flags: Intents): Intents = Intents(this.code - flags.code)

public inline fun copy(block: Builder.() -> Unit): Intents {
contract { callsInPlace(block, EXACTLY_ONCE) }
return Builder(code).apply(block).flags()
}

override fun equals(other: Any?): Boolean = this === other ||
(other is Intents && this.code == other.code)

Expand Down Expand Up @@ -167,11 +172,6 @@ public fun Intents(flags: Iterable<Intent>): Intents = Intents { flags.forEach {
@JvmName("Intents0")
public fun Intents(flags: Iterable<Intents>): Intents = Intents { flags.forEach { +it } }

public inline fun Intents.copy(block: Intents.Builder.() -> Unit): Intents {
contract { callsInPlace(block, EXACTLY_ONCE) }
return Intents.Builder(code).apply(block).flags()
}

/**
* Values that enable a group of events as [defined by
* Discord](https://discord.com/developers/docs/topics/gateway#gateway-intents).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ internal fun BitFlags.generateFileSpec(originatingFile: KSFile) = fileSpecForGen
collectionName,
)
}
addFunction("copy") {
addModifiers(PUBLIC, INLINE)
addParameter("block", type = LambdaTypeName.get(receiver = builderName, returnType = UNIT))
returns(collectionName)
addStatement("%M { callsInPlace(block, %M) }", CONTRACT, EXACTLY_ONCE)
addStatement("return %T($valueName).apply(block).flags()", builderName)
}
addEqualsAndHashCode(collectionName)
addFunction("toString") {
addModifiers(OVERRIDE)
Expand All @@ -117,7 +124,6 @@ internal fun BitFlags.generateFileSpec(originatingFile: KSFile) = fileSpecForGen
addSerializer(collectionName)
}
addFactoryFunctions(collectionName, builderName)
addCopyFunction(collectionName, builderName)
addClass(entityCN) {
// for ksp incremental processing
addOriginatingKSFile(originatingFile)
Expand Down Expand Up @@ -235,16 +241,3 @@ private fun FileSpec.Builder.addFactoryFunctions(collectionName: ClassName, buil
addStatement("return $factoryFunctionName·{ flags.forEach·{ +it } }")
}
}

context(BitFlags)
private fun FileSpec.Builder.addCopyFunction(collectionName: ClassName, builderName: ClassName) {
addFunction("copy") {
addModifiers(PUBLIC, INLINE)
receiver(collectionName)
addParameter("block", type = LambdaTypeName.get(receiver = builderName, returnType = UNIT))
returns(collectionName)

addStatement("%M { callsInPlace(block, %M) }", CONTRACT, EXACTLY_ONCE)
addStatement("return %T($valueName).apply(block).flags()", builderName)
}
}
Loading

0 comments on commit 6c29dc3

Please sign in to comment.