@@ -53,11 +53,18 @@ open class NumberFormatter : Formatter, @unchecked Sendable {
5353 super. init ( coder: coder)
5454 }
5555
56- // Consumes state
57- private convenience init ( state: State ) {
56+ private convenience init ( state: consuming sending State) {
5857 self . init ( )
59- nonisolated ( unsafe) let consumedState = state
60- _lock. withLock { $0 = consumedState }
58+
59+ // work around issue that state needs to be reinitialized after consuming
60+ struct Wrapper : ~ Copyable, @unchecked Sendable {
61+ var value : State ? = nil
62+ }
63+ var w = Wrapper ( value: consume state)
64+
65+ _lock. withLock {
66+ $0 = w. value. take ( ) !
67+ }
6168 }
6269
6370 open override func copy( with zone: NSZone ? = nil ) -> Any {
@@ -74,98 +81,91 @@ open class NumberFormatter : Formatter, @unchecked Sendable {
7481 return numberFormatter. string ( for: num) !
7582 }
7683
77- final class State {
78- private var _formatter : CFNumberFormatter ? = nil
84+ struct State : ~ Copyable {
85+ class Box {
86+ var formatter : CFNumberFormatter ?
87+ init ( ) { }
88+ }
89+
90+ private var _formatter = Box ( )
7991
8092 // MARK: -
8193
8294 func copy( with zone: NSZone ? = nil ) -> State {
83- let copied = State ( )
84-
85- func __copy< T> ( _ keyPath: ReferenceWritableKeyPath < State , T > ) {
86- copied [ keyPath: keyPath] = self [ keyPath: keyPath]
87- }
88-
89- func __copy< T> ( _ keyPath: ReferenceWritableKeyPath < State , T > ) where T: NSCopying {
90- copied [ keyPath: keyPath] = self [ keyPath: keyPath] . copy ( with: zone) as! T
91- }
92-
93- func __copy< T> ( _ keyPath: ReferenceWritableKeyPath < State , T ? > ) where T: NSCopying {
94- copied [ keyPath: keyPath] = self [ keyPath: keyPath] ? . copy ( with: zone) as! T ?
95- }
96-
97- __copy ( \. formattingContext)
98- __copy ( \. _numberStyle)
99- __copy ( \. _locale)
100- __copy ( \. _generatesDecimalNumbers)
101- __copy ( \. _textAttributesForNegativeValues)
102- __copy ( \. _textAttributesForPositiveValues)
103- __copy ( \. _allowsFloats)
104- __copy ( \. _decimalSeparator)
105- __copy ( \. _alwaysShowsDecimalSeparator)
106- __copy ( \. _currencyDecimalSeparator)
107- __copy ( \. _usesGroupingSeparator)
108- __copy ( \. _groupingSeparator)
109- __copy ( \. _zeroSymbol)
110- __copy ( \. _textAttributesForZero)
111- __copy ( \. _nilSymbol)
112- __copy ( \. _textAttributesForNil)
113- __copy ( \. _notANumberSymbol)
114- __copy ( \. _textAttributesForNotANumber)
115- __copy ( \. _positiveInfinitySymbol)
116- __copy ( \. _textAttributesForPositiveInfinity)
117- __copy ( \. _negativeInfinitySymbol)
118- __copy ( \. _textAttributesForNegativeInfinity)
119- __copy ( \. _positivePrefix)
120- __copy ( \. _positiveSuffix)
121- __copy ( \. _negativePrefix)
122- __copy ( \. _negativeSuffix)
123- __copy ( \. _currencyCode)
124- __copy ( \. _currencySymbol)
125- __copy ( \. _internationalCurrencySymbol)
126- __copy ( \. _percentSymbol)
127- __copy ( \. _perMillSymbol)
128- __copy ( \. _minusSign)
129- __copy ( \. _plusSign)
130- __copy ( \. _exponentSymbol)
131- __copy ( \. _groupingSize)
132- __copy ( \. _secondaryGroupingSize)
133- __copy ( \. _multiplier)
134- __copy ( \. _formatWidth)
135- __copy ( \. _paddingCharacter)
136- __copy ( \. _paddingPosition)
137- __copy ( \. _roundingMode)
138- __copy ( \. _roundingIncrement)
139- __copy ( \. _minimumIntegerDigits)
140- __copy ( \. _maximumIntegerDigits)
141- __copy ( \. _minimumFractionDigits)
142- __copy ( \. _maximumFractionDigits)
143- __copy ( \. _minimum)
144- __copy ( \. _maximum)
145- __copy ( \. _currencyGroupingSeparator)
146- __copy ( \. _lenient)
147- __copy ( \. _usesSignificantDigits)
148- __copy ( \. _minimumSignificantDigits)
149- __copy ( \. _maximumSignificantDigits)
150- __copy ( \. _partialStringValidationEnabled)
151- __copy ( \. _hasThousandSeparators)
152- __copy ( \. _thousandSeparator)
153- __copy ( \. _localizesFormat)
154- __copy ( \. _positiveFormat)
155- __copy ( \. _negativeFormat)
156- __copy ( \. _roundingBehavior)
95+ var copied = State ( )
96+
97+ copied. formattingContext = formattingContext
98+ copied. _numberStyle = _numberStyle
99+ copied. _locale = _locale
100+ copied. _generatesDecimalNumbers = _generatesDecimalNumbers
101+ copied. _textAttributesForNegativeValues = _textAttributesForNegativeValues
102+ copied. _textAttributesForPositiveValues = _textAttributesForPositiveValues
103+ copied. _allowsFloats = _allowsFloats
104+ copied. _decimalSeparator = _decimalSeparator
105+ copied. _alwaysShowsDecimalSeparator = _alwaysShowsDecimalSeparator
106+ copied. _currencyDecimalSeparator = _currencyDecimalSeparator
107+ copied. _usesGroupingSeparator = _usesGroupingSeparator
108+ copied. _groupingSeparator = _groupingSeparator
109+ copied. _zeroSymbol = _zeroSymbol
110+ copied. _textAttributesForZero = _textAttributesForZero
111+ copied. _nilSymbol = _nilSymbol
112+ copied. _textAttributesForNil = _textAttributesForNil
113+ copied. _notANumberSymbol = _notANumberSymbol
114+ copied. _textAttributesForNotANumber = _textAttributesForNotANumber
115+ copied. _positiveInfinitySymbol = _positiveInfinitySymbol
116+ copied. _textAttributesForPositiveInfinity = _textAttributesForPositiveInfinity
117+ copied. _negativeInfinitySymbol = _negativeInfinitySymbol
118+ copied. _textAttributesForNegativeInfinity = _textAttributesForNegativeInfinity
119+ copied. _positivePrefix = _positivePrefix
120+ copied. _positiveSuffix = _positiveSuffix
121+ copied. _negativePrefix = _negativePrefix
122+ copied. _negativeSuffix = _negativeSuffix
123+ copied. _currencyCode = _currencyCode
124+ copied. _currencySymbol = _currencySymbol
125+ copied. _internationalCurrencySymbol = _internationalCurrencySymbol
126+ copied. _percentSymbol = _percentSymbol
127+ copied. _perMillSymbol = _perMillSymbol
128+ copied. _minusSign = _minusSign
129+ copied. _plusSign = _plusSign
130+ copied. _exponentSymbol = _exponentSymbol
131+ copied. _groupingSize = _groupingSize
132+ copied. _secondaryGroupingSize = _secondaryGroupingSize
133+ copied. _multiplier = _multiplier
134+ copied. _formatWidth = _formatWidth
135+ copied. _paddingCharacter = _paddingCharacter
136+ copied. _paddingPosition = _paddingPosition
137+ copied. _roundingMode = _roundingMode
138+ copied. _roundingIncrement = _roundingIncrement
139+ copied. _minimumIntegerDigits = _minimumIntegerDigits
140+ copied. _maximumIntegerDigits = _maximumIntegerDigits
141+ copied. _minimumFractionDigits = _minimumFractionDigits
142+ copied. _maximumFractionDigits = _maximumFractionDigits
143+ copied. _minimum = _minimum
144+ copied. _maximum = _maximum
145+ copied. _currencyGroupingSeparator = _currencyGroupingSeparator
146+ copied. _lenient = _lenient
147+ copied. _usesSignificantDigits = _usesSignificantDigits
148+ copied. _minimumSignificantDigits = _minimumSignificantDigits
149+ copied. _maximumSignificantDigits = _maximumSignificantDigits
150+ copied. _partialStringValidationEnabled = _partialStringValidationEnabled
151+ copied. _hasThousandSeparators = _hasThousandSeparators
152+ copied. _thousandSeparator = _thousandSeparator
153+ copied. _localizesFormat = _localizesFormat
154+ copied. _positiveFormat = _positiveFormat
155+ copied. _negativeFormat = _negativeFormat
156+ copied. _roundingBehavior = _roundingBehavior
157157
158158 return copied
159159 }
160160
161161 // MARK: -
162162
163163 func _reset( ) {
164- _formatter = nil
164+ _formatter. formatter = nil
165165 }
166166
167167 func formatter( ) -> CFNumberFormatter {
168- if let obj = _formatter {
168+ if let obj = _formatter. formatter {
169169 return obj
170170 } else {
171171 let numberStyle = CFNumberFormatterStyle ( rawValue: CFIndex ( _numberStyle. rawValue) ) !
@@ -180,7 +180,7 @@ open class NumberFormatter : Formatter, @unchecked Sendable {
180180 }
181181 CFNumberFormatterSetFormat ( obj, format. _cfObject)
182182 }
183- _formatter = obj
183+ _formatter. formatter = obj
184184 return obj
185185 }
186186 }
0 commit comments