Repository navigation
I2C/SPI Read should not allow empty reads - #278
Conversation
| /// </param> | ||
| public override unsafe void Read(Span<byte> buffer) | ||
| { | ||
| if (buffer.Length == 0) |
There was a problem hiding this comment.
Length [](start = 23, length = 6)
why we throw? should we just return? I prefer not throwing.
There was a problem hiding this comment.
There was a problem hiding this comment.
zero length is pretty much always a bug on the caller side - why read nothing?
There was a problem hiding this comment.
I would suggest to have this method return the read count instead of void and don't throw here. return 0 for 0 length buffer is good indication what could be the issue.
My point is, this is a public API and you never know the caller scenario which may end up passing zero length buffer. throwing exception is always a headache in such scenarios. we have similar notion like in Encoding and passing empty string, just a valid input and we don't throw.
In reply to: 259636817 [](ancestors = 259636817)
There was a problem hiding this comment.
@tarekgh this is slightly different to regular Stream Reads/Writes - you have to know up front how many bytes you want to read and scenarios where you don't read the whole buffer are unexpected cc: @JeremyKuhne to double check I'm not saying anything incorrectly
There was a problem hiding this comment.
@tarekgh I do not believe there currently any devices which support reading like that - currently pretty much every device I worked with required writing an address just before doing any read and number of bytes is known in the specification - I don't think there are any scenarios for this kind of streaming here
There was a problem hiding this comment.
Are you saying, the span length always has to match the number of bytes we read? this doesn't sound right to me.
There was a problem hiding this comment.
@tarekgh I'm almost sure that is this case. I think considering the current shape of the API the empty read does not make sense - we can reiterate on the return value of this API later - does that sound reasonable? So far I haven't seen a case where you don't know much about what the other device will send you
There was a problem hiding this comment.
go ahead and don't block on my comment. Just make sure you track discussing this more and get some feedback if we need to change the behavior. thanks!
Fixes #111
Making experience better when empty buffer is accidentally passed to Read