Re: [swift-users] Extracting arbitrary types (e.g. UInt16) out of Data

2017-06-25 Thread Roderick Mann via swift-users
I mean, it's as straightforward as my example. I have a Data of arbitrary size 
(anywhere from 3 to 29 bytes, let's say). The last two bytes form a UInt16 CRC. 
I need to get those last two out and compare them against the CRC I compute for 
the rest of the bytes.

Having said that, I just used withUnsafeBytes() and grabbed the last two bytes, 
and assembled them into a UInt16 with shift and or.

I'd like to be able to do something like value(at: 3), though.


> On Jun 25, 2017, at 19:53 , Philippe Hausler  wrote:
> 
> There are probably a number of ways that would do what you need. I would need 
> a bit more context or examples of what you are doing already to comment. But 
> if I had those parameters to work with I would use copyBytes into the address 
> of the target you are wanting to read.
> 
> There are some cases that might be improved when we add the 
> UnsafeRawBuferPointer apis to Data.
> 
> Can you share a small sample of what you have already?
> 
>> On Jun 25, 2017, at 5:37 PM, Rick Mann via swift-users 
>>  wrote:
>> 
>> I continue to struggle with the "proper" and most efficient way to do things 
>> with Data.
>> 
>> In this case, I have a set of bytes received over a serial port in a Data. 
>> The last two bytes are a (big- or little-endian) UInt16 CRC. However, there 
>> maybe an odd or even number of bytes in the Data before these last two 
>> bytes, so I can't just use withUnsafePointer.
>> 
>> I'd also like to avoid unnecessary copying of the data. All of it is 
>> immutable for the purposes of this problem.
>> 
>> How can I get the UInt16 that starts at byte X in a Data? Same goes for 
>> Double or Int32 or whatever.
>> 
>> If the endianness needs to change, I can do that swapping after I've gotten 
>> the typed value out.
>> 
>> -- 
>> Rick Mann
>> rm...@latencyzero.com
>> 
>> 
>> ___
>> swift-users mailing list
>> swift-users@swift.org
>> https://lists.swift.org/mailman/listinfo/swift-users
> 


-- 
Rick Mann
rm...@latencyzero.com


___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] Covariance in Generic Protocols & Proposal 0142

2017-06-25 Thread Zhao Xin via swift-users
`protocol A: GenericProtocol where Instance == String { }` means, A is
something that `Instance` must be `String`.
However, it doesn't mean `Instance` has already been `String`.

So without assigning your `Instance`, the compiler doesn't know the type of
your `Instance`.

Zhao Xin

On Mon, Jun 26, 2017 at 11:46 AM, Justin Jia via swift-users <
swift-users@swift.org> wrote:

> Hi,
>
> I’m trying to implement something like this using Swift 4 after proposal
> 0142 is implemented:
>
> ```
> protocol GenericProtocol {
> associatedtype Instance
> func foo() -> Instance
> func bar() -> Instance
> // and more...
> }
>
> protocol A: GenericProtocol where Instance == String { }
> protocol B: GenericProtocol where Instance == Int { }
> protocol C: GenericProtocol where Instance == Double { }
> // and more…
>
> class Bar {
> var a: A // Error: Protocol ‘A' can only be used as a generic
> constraint because it has Self or associated type requirements
> var b: B // Error: Protocol ‘B' can only be used as a generic
> constraint because it has Self or associated type requirements
> var c: C // Error: Protocol ‘C' can only be used as a generic
> constraint because it has Self or associated type requirements
> // and more...
> }
> ```
>
> However, I’m still getting the `Protocol ‘A' can only be used as a generic
> constraint because it has Self or associated type requirements` error.
>
> Instead, the only thing I can do right now is to duplicate my code:
>
> (Just in case your are wondering, I need this syntax to simplify some code
> I’m working on https://github.com/TintPoint/Overlay/tree/swift-4.0)
>
> ```
> protocol A {
> func foo() -> String
> func bar() -> String
> // and more...
> }
>
> protocol B {
> func foo() -> Int
> func bar() -> Int
> // and more...
> }
>
> protocol B {
> func foo() -> Double
> func bar() -> Double
> // and more...
> }
>
> // and more...
>
> class Bar {
> var a: A // OK
> var b: B // OK
> var c: C // OK
> // and more...
> }
> ```
>
> Am I doing something wrong here? Is it Swift’s current limitation that can
> be improved in a future version of Swift? Or it needs a special syntax and
> a separate proposal?
>
> Thank you in advance for your help!
>
> Sincerely,
> Justin
>
> ___
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
>
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


[swift-users] TWISt-shout Newsletter 2017-06-26

2017-06-25 Thread Kenny Leung via swift-users

Hi All.

Here is your TWISt-shout Newsletter for the week of 2017-06-19 to 2017-06-25

https://github.com/pepperdog/TWISt-shout/blob/master/2017/TWISt-shout-2017-06-26.md
 


Enjoy!

-Kenny


___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] Extracting arbitrary types (e.g. UInt16) out of Data

2017-06-25 Thread Philippe Hausler via swift-users
There are probably a number of ways that would do what you need. I would need a 
bit more context or examples of what you are doing already to comment. But if I 
had those parameters to work with I would use copyBytes into the address of the 
target you are wanting to read.

There are some cases that might be improved when we add the 
UnsafeRawBuferPointer apis to Data.

Can you share a small sample of what you have already?

> On Jun 25, 2017, at 5:37 PM, Rick Mann via swift-users 
>  wrote:
> 
> I continue to struggle with the "proper" and most efficient way to do things 
> with Data.
> 
> In this case, I have a set of bytes received over a serial port in a Data. 
> The last two bytes are a (big- or little-endian) UInt16 CRC. However, there 
> maybe an odd or even number of bytes in the Data before these last two bytes, 
> so I can't just use withUnsafePointer.
> 
> I'd also like to avoid unnecessary copying of the data. All of it is 
> immutable for the purposes of this problem.
> 
> How can I get the UInt16 that starts at byte X in a Data? Same goes for 
> Double or Int32 or whatever.
> 
> If the endianness needs to change, I can do that swapping after I've gotten 
> the typed value out.
> 
> -- 
> Rick Mann
> rm...@latencyzero.com
> 
> 
> ___
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


[swift-users] Extracting arbitrary types (e.g. UInt16) out of Data

2017-06-25 Thread Rick Mann via swift-users
I continue to struggle with the "proper" and most efficient way to do things 
with Data.

In this case, I have a set of bytes received over a serial port in a Data. The 
last two bytes are a (big- or little-endian) UInt16 CRC. However, there maybe 
an odd or even number of bytes in the Data before these last two bytes, so I 
can't just use withUnsafePointer.

I'd also like to avoid unnecessary copying of the data. All of it is immutable 
for the purposes of this problem.

How can I get the UInt16 that starts at byte X in a Data? Same goes for Double 
or Int32 or whatever.

If the endianness needs to change, I can do that swapping after I've gotten the 
typed value out.

-- 
Rick Mann
rm...@latencyzero.com


___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


[swift-users] Swift 4 thread sanitizer more sensitive?

2017-06-25 Thread Jon Shier via swift-users
Running Alamofire through the thread sanitizer and the thread sanitizer 
finds issues in Swift 4 mode but not Swift 3.2. Does Swift 4 add additional 
threading instrumentation that you don’t get under other versions? Or is there 
some other runtime difference that explains it?



Jon Shier
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users