Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

machine: add no-op io.Reader implementation to NullSerial #2593

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

bgould
Copy link
Member

@bgould bgould commented Jan 29, 2022

When compiling with -serial=usb or -serial=uart, machine.Serial implements drivers.UART.

Currently the same is not true for -serial=none, because machine.NullSerial does not implement io.Reader as necessary: https://github.com/tinygo-org/drivers/blob/a15f2167cc7e46864d82e08904a3ddaa3e610d5e/uart.go#L8

Adding a no-op Read() method so that NullSerial implements drivers.UART

@bgould bgould changed the title machine: add no-op io.Reader implement to NullSerial machine: add no-op io.Reader implementation to NullSerial Jan 29, 2022
@bgould bgould force-pushed the serial-none-noop-read branch 4 times, most recently from 7114854 to d5916cb Compare January 29, 2022 19:35
@bgould bgould force-pushed the serial-none-noop-read branch from d5916cb to 277b3f7 Compare January 29, 2022 20:19
@bgould
Copy link
Member Author

bgould commented Jan 29, 2022

renamed the unexported io struct in machine_rp2040_gpio.go to avoid naming collision with io package


// Read is a no-op; always returns that zero bytes were read with no error
func (ns NullSerial) Read(p []byte) (n int, err error) {
return 0, nil
Copy link

Choose a reason for hiding this comment

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

Just wondering, shouldn't it return io.EOF to show that there is nothing to read?

As per doc

// Implementations of Read are discouraged from returning a
// zero byte count with a nil error, except when len(p) == 0.
// Callers should treat a return of 0 and nil as indicating that
// nothing happened; in particular it does not indicate EOF.

The same as ReadByte() always returns an error

func (ns NullSerial) ReadByte() (byte, error) {
return 0, errNoByte
}

Copy link
Member

Choose a reason for hiding this comment

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

Agreed, returning an error seems more correct to me.

Copy link
Member Author

Choose a reason for hiding this comment

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

Do you think should return io.EOF or should it return errNoByte (the same as ReadByte)?


// Read is a no-op; always returns that zero bytes were read with no error
func (ns NullSerial) Read(p []byte) (n int, err error) {
return 0, nil
Copy link
Member

Choose a reason for hiding this comment

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

Agreed, returning an error seems more correct to me.

Comment on lines +56 to +59
// type check to ensure NullSerial implements interfaces for drivers.UART
var (
_ io.Reader = NullSerial{}
_ io.Writer = NullSerial{}
Copy link
Member

Choose a reason for hiding this comment

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

👍

@@ -22,7 +22,7 @@ type irqCtrl struct {
}

type ioBank0Type struct {
io [30]io
io [30]rpio
Copy link
Member

Choose a reason for hiding this comment

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

Was this change intentional?

Copy link
Member Author

@bgould bgould Feb 6, 2022

Choose a reason for hiding this comment

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

Importing the io package as part of the type check causes a compile error in the smoke test for RP2040 because it defines an internal type called io as well. The alternative would have been to import io with an alias in serial.go, but I thought it would be better and more appropriate to rename the type for the rp2040 to prevent this problem from happening again in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants