@@ -16,6 +16,10 @@ import kotlinx.collections.immutable.implementations.persistentOrderedMap.Persis
1616import kotlinx.collections.immutable.implementations.persistentOrderedMap.PersistentOrderedMapBuilder
1717import kotlinx.collections.immutable.implementations.persistentOrderedSet.PersistentOrderedSet
1818import kotlinx.collections.immutable.implementations.persistentOrderedSet.PersistentOrderedSetBuilder
19+ import kotlin.contracts.ExperimentalContracts
20+ import kotlin.contracts.InvocationKind
21+ import kotlin.contracts.contract
22+ import kotlin.experimental.ExperimentalTypeInference
1923
2024// @Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
2125// inline fun <T> @kotlin.internal.Exact ImmutableCollection<T>.mutate(mutator: (MutableCollection<T>) -> Unit): ImmutableCollection<T> = builder().apply(mutator).build()
@@ -766,3 +770,76 @@ public fun <K, V> Map<K, V>.toPersistentHashMap(): PersistentMap<K, V>
766770 = this as ? PersistentHashMap
767771 ? : (this as ? PersistentHashMapBuilder <K , V >)?.build()
768772 ? : PersistentHashMap .emptyOf<K , V >().putAll(this )
773+
774+ /* *
775+ * Builds a new [PersistentList] by populating a [PersistentList.Builder] using the given [builderAction]
776+ * and returning an immutable list with the same elements.
777+ *
778+ * The list passed as a receiver to the [builderAction] is valid only inside that function.
779+ * Using it outside the function produces an unspecified behavior.
780+ */
781+ @OptIn(ExperimentalTypeInference ::class , ExperimentalContracts ::class )
782+ public inline fun <T > buildPersistentList (@BuilderInference builderAction : PersistentList .Builder <T >.() -> Unit ): PersistentList <T > {
783+ contract { callsInPlace(builderAction, InvocationKind .EXACTLY_ONCE ) }
784+ return persistentListOf<T >().builder().apply (builderAction).build()
785+ }
786+
787+ /* *
788+ * Builds a new [PersistentSet] by populating a [PersistentSet.Builder] using the given [builderAction]
789+ * and returning an immutable set with the same elements.
790+ *
791+ * The set passed as a receiver to the [builderAction] is valid only inside that function.
792+ * Using it outside the function produces an unspecified behavior.
793+ *
794+ * Elements of the set are iterated in the order they were added by the [builderAction].
795+ */
796+ @OptIn(ExperimentalTypeInference ::class , ExperimentalContracts ::class )
797+ public inline fun <T > buildPersistentSet (@BuilderInference builderAction : PersistentSet .Builder <T >.() -> Unit ): PersistentSet <T > {
798+ contract { callsInPlace(builderAction, InvocationKind .EXACTLY_ONCE ) }
799+ return persistentSetOf<T >().builder().apply (builderAction).build()
800+ }
801+
802+ /* *
803+ * Builds a new [PersistentSet] by populating a [PersistentSet.Builder] using the given [builderAction]
804+ * and returning an immutable set with the same elements.
805+ *
806+ * The set passed as a receiver to the [builderAction] is valid only inside that function.
807+ * Using it outside the function produces an unspecified behavior.
808+ *
809+ * Order of the elements in the returned set is unspecified.
810+ */
811+ @OptIn(ExperimentalTypeInference ::class , ExperimentalContracts ::class )
812+ public inline fun <T > buildPersistentHashSet (@BuilderInference builderAction : PersistentSet .Builder <T >.() -> Unit ): PersistentSet <T > {
813+ contract { callsInPlace(builderAction, InvocationKind .EXACTLY_ONCE ) }
814+ return persistentHashSetOf<T >().builder().apply (builderAction).build()
815+ }
816+
817+ /* *
818+ * Builds a new [PersistentMap] by populating a [PersistentMap.Builder] using the given [builderAction]
819+ * and returning an immutable map with the same key-value pairs.
820+ *
821+ * The map passed as a receiver to the [builderAction] is valid only inside that function.
822+ * Using it outside the function produces an unspecified behavior.
823+ *
824+ * Entries of the map are iterated in the order they were added by the [builderAction].
825+ */
826+ @OptIn(ExperimentalTypeInference ::class , ExperimentalContracts ::class )
827+ public inline fun <K , V > buildPersistentMap (@BuilderInference builderAction : PersistentMap .Builder <K , V >.() -> Unit ): PersistentMap <K , V > {
828+ contract { callsInPlace(builderAction, InvocationKind .EXACTLY_ONCE ) }
829+ return persistentMapOf<K , V >().builder().apply (builderAction).build()
830+ }
831+
832+ /* *
833+ * Builds a new [PersistentMap] by populating a [PersistentMap.Builder] using the given [builderAction]
834+ * and returning an immutable map with the same key-value pairs.
835+ *
836+ * The map passed as a receiver to the [builderAction] is valid only inside that function.
837+ * Using it outside the function produces an unspecified behavior.
838+ *
839+ * Order of the entries in the returned map is unspecified.
840+ */
841+ @OptIn(ExperimentalTypeInference ::class , ExperimentalContracts ::class )
842+ public inline fun <K , V > buildPersistentHashMap (@BuilderInference builderAction : PersistentMap .Builder <K , V >.() -> Unit ): PersistentMap <K , V > {
843+ contract { callsInPlace(builderAction, InvocationKind .EXACTLY_ONCE ) }
844+ return persistentHashMapOf<K , V >().builder().apply (builderAction).build()
845+ }
0 commit comments