Skip to content

I2C/SPI Read should not allow empty reads - #278

Merged
krwq merged 1 commit into
dotnet:masterfrom
krwq:i2cemptyread
Feb 26, 2019
Merged

krwq merged 1 commit into
dotnet:masterfrom
krwq:i2cemptyread

Conversation

@krwq

@krwq krwq commented Feb 24, 2019

Copy link
Copy Markdown
Member

Fixes #111

Making experience better when empty buffer is accidentally passed to Read

@krwq
krwq requested review from JeremyKuhne and tarekgh February 24, 2019 06:51
/// </param>
public override unsafe void Read(Span<byte> buffer)
{
if (buffer.Length == 0)

@tarekgh tarekgh Feb 24, 2019 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Length [](start = 23, length = 6)

why we throw? should we just return? I prefer not throwing.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this comment apply in all changed places.


In reply to: 259633550 [](ancestors = 259633550)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

zero length is pretty much always a bug on the caller side - why read nothing?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you saying, the span length always has to match the number of bytes we read? this doesn't sound right to me.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

created #288 to track this

@krwq
krwq merged commit 628a25d into dotnet:master Feb 26, 2019
@github-actions github-actions Bot locked and limited conversation to collaborators Dec 14, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants