@@ -30,7 +30,7 @@ extension ShadowDecoder {
3030 case 2 :
3131 let key = ( row: decoder. codingPath [ 0 ] , field: decoder. codingPath [ 1 ] )
3232 let r = try key. row. intValue ?> CSVDecoder . Error. _invalidRowKey ( forKey: key. row, codingPath: decoder. codingPath)
33- let f = try decoder. source. fieldIndex ( forKey: key. field, codingPath: decoder. codingPath)
33+ let f = try decoder. source. _withUnsafeGuaranteedRef { try $0 . fieldIndex ( forKey: key. field, codingPath: decoder. codingPath) }
3434 self . _focus = . field( r, f)
3535 case 1 :
3636 let key = decoder. codingPath [ 0 ]
@@ -56,14 +56,14 @@ extension ShadowDecoder.SingleValueContainer {
5656 }
5757
5858 func decodeNil( ) -> Bool {
59- switch self . _decoder. source. configuration. nilStrategy {
59+ switch self . _decoder. source. _withUnsafeGuaranteedRef ( { $0 . configuration. nilStrategy } ) {
6060 case . empty: return ( try ? self . _lowlevelDecode { $0. isEmpty } ) ?? false
6161 case . custom( let closure) : return closure ( self . _decoder)
6262 }
6363 }
6464
6565 func decode( _ type: Bool . Type ) throws -> Bool {
66- switch self . _decoder. source. configuration. boolStrategy {
66+ switch self . _decoder. source. _withUnsafeGuaranteedRef ( { $0 . configuration. boolStrategy } ) {
6767 case . deferredToBool:
6868 return try self . _lowlevelDecode { Bool ( $0) }
6969 case . insensitive:
@@ -122,7 +122,7 @@ extension ShadowDecoder.SingleValueContainer {
122122 func decode( _ type: Float . Type ) throws -> Float {
123123 try self . _lowlevelDecode {
124124 guard let result = Float ( $0) , result. isFinite else {
125- switch self . _decoder. source. configuration. nonConformingFloatStrategy {
125+ switch self . _decoder. source. _withUnsafeGuaranteedRef ( { $0 . configuration. nonConformingFloatStrategy } ) {
126126 case . throw: return nil
127127 case . convert( let positiveInfinity, let negativeInfinity, let nan) :
128128 switch $0 {
@@ -141,7 +141,7 @@ extension ShadowDecoder.SingleValueContainer {
141141 func decode( _ type: Double . Type ) throws -> Double {
142142 try self . _lowlevelDecode {
143143 guard let result = Double ( $0) , result. isFinite else {
144- switch self . _decoder. source. configuration. nonConformingFloatStrategy {
144+ switch self . _decoder. source. _withUnsafeGuaranteedRef ( { $0 . configuration. nonConformingFloatStrategy } ) {
145145 case . throw: return nil
146146 case . convert( let positiveInfinity, let negativeInfinity, let nan) :
147147 switch $0 {
@@ -173,7 +173,7 @@ extension ShadowDecoder.SingleValueContainer {
173173 /// - parameter type: The type to decode as.
174174 /// - returns: A value of the requested type.
175175 func decode( _ type: Date . Type ) throws -> Date {
176- switch self . _decoder. source. configuration. dateStrategy {
176+ switch self . _decoder. source. _withUnsafeGuaranteedRef ( { $0 . configuration. dateStrategy } ) {
177177 case . deferredToDate:
178178 return try Date ( from: self . _decoder)
179179 case . secondsSince1970:
@@ -197,7 +197,7 @@ extension ShadowDecoder.SingleValueContainer {
197197 /// - parameter type: The type to decode as.
198198 /// - returns: A value of the requested type.
199199 func decode( _ type: Data . Type ) throws -> Data {
200- switch self . _decoder. source. configuration. dataStrategy {
200+ switch self . _decoder. source. _withUnsafeGuaranteedRef ( { $0 . configuration. dataStrategy } ) {
201201 case . deferredToData:
202202 return try Data ( from: self . _decoder)
203203 case . base64:
@@ -212,7 +212,7 @@ extension ShadowDecoder.SingleValueContainer {
212212 /// - parameter type: The type to decode as.
213213 /// - returns: A value of the requested type.
214214 func decode( _ type: Decimal . Type ) throws -> Decimal {
215- switch self . _decoder. source. configuration. decimalStrategy {
215+ switch self . _decoder. source. _withUnsafeGuaranteedRef ( { $0 . configuration. decimalStrategy } ) {
216216 case . locale( let locale) :
217217 let string = try self . decode ( String . self)
218218 return try Decimal ( string: string, locale: locale) ?> CSVDecoder . Error. _invalidDecimal ( string: string, locale: locale, codingPath: self . codingPath)
@@ -245,24 +245,24 @@ private extension ShadowDecoder.SingleValueContainer {
245245 /// Decodes the `String` value under the receiving single value container's `focus` and then tries to transform it in the requested type.
246246 /// - parameter transform: Closure transforming the decoded `String` value into the required type. If it fails, the closure returns `nil`.
247247 func _lowlevelDecode< T> ( transform: ( String ) -> T ? ) throws -> T {
248- let source = self . _decoder. source
249-
250- switch self . _focus {
251- case . field( let rowIndex, let fieldIndex) :
252- let string = try source . field ( rowIndex , fieldIndex )
253- return try transform ( string ) ?> CSVDecoder . Error . _invalid ( type : T . self , string : string , codingPath : self . codingPath )
254- case . row( let rowIndex ) :
255- // Values are only allowed to be decoded directly from a single value container in "row level" if the CSV has single column rows.
256- guard source . numExpectedFields == 1 else { throw CSVDecoder . Error . _invalidNestedRequired ( codingPath : self . codingPath ) }
257- let string = try source . field ( rowIndex , 0 )
258- return try transform ( string ) ?> CSVDecoder . Error . _invalid ( type : T . self , string : string , codingPath : self . codingPath + [ IndexKey ( 0 ) ] )
259- case . file:
260- // Values are only allowed to be decoded directly from a single value container in "file level" if the CSV file has a single row with a single column.
261- if source . isRowAtEnd ( index : 1 ) , source . numExpectedFields == 1 {
262- let string = try self . _decoder . source . field ( 0 , 0 )
263- return try transform ( string ) ?> CSVDecoder . Error . _invalid ( type : T . self , string : string , codingPath : self . codingPath + [ IndexKey ( 0 ) , IndexKey ( 0 ) ] )
264- } else {
265- throw CSVDecoder . Error . _invalidNestedRequired ( codingPath : self . codingPath )
248+ try self . _decoder. source. _withUnsafeGuaranteedRef {
249+ switch self . _focus {
250+ case . field ( let rowIndex , let fieldIndex ) :
251+ let string = try $0 . field ( rowIndex, fieldIndex)
252+ return try transform ( string ) ?> CSVDecoder . Error . _invalid ( type : T . self , string : string , codingPath : self . codingPath )
253+ case . row ( let rowIndex ) :
254+ // Values are only allowed to be decoded directly from a single value container in " row level" if the CSV has single column rows.
255+ guard $0 . numExpectedFields == 1 else { throw CSVDecoder . Error . _invalidNestedRequired ( codingPath : self . codingPath ) }
256+ let string = try $0 . field ( rowIndex , 0 )
257+ return try transform ( string ) ?> CSVDecoder . Error . _invalid ( type : T . self , string : string , codingPath : self . codingPath + [ IndexKey ( 0 ) ] )
258+ case . file :
259+ // Values are only allowed to be decoded directly from a single value container in " file level" if the CSV file has a single row with a single column.
260+ if $0 . isRowAtEnd ( index : 1 ) , $0 . numExpectedFields == 1 {
261+ let string = try $0 . field ( 0 , 0 )
262+ return try transform ( string ) ?> CSVDecoder . Error . _invalid ( type : T . self , string : string , codingPath : self . codingPath + [ IndexKey ( 0 ) , IndexKey ( 0 ) ] )
263+ } else {
264+ throw CSVDecoder . Error . _invalidNestedRequired ( codingPath : self . codingPath )
265+ }
266266 }
267267 }
268268 }
0 commit comments