zhboner / realm

A network relay tool
MIT License
1.48k stars 279 forks source link

io: return bytes read/write in copy #95

Closed howardjohn closed 1 year ago

howardjohn commented 1 year ago

This matches the behavior of std and Tokio copy methods, and allows doing things like reporting telemetry based on how much data is copied, for example.

The implementation follows tokio's bidi copy implementation.

zephyrchien commented 1 year ago

I'm ok with this PR. But I'm not sure if the return value here is useful.

When error occurs, the returned Result reports Error, instead of transferred bytes. Personally I perfer to implement a custom Reader or Writer which meets those type constraints, to count transferred bytes in-time and do some custom sync operations on demand.

howardjohn commented 1 year ago

That's an interesting point. I was copying from https://docs.rs/tokio/latest/tokio/io/fn.copy_bidirectional.html but hadn't considered that. The Go version (https://pkg.go.dev/io#Copy) returns an error and bytes transferred because it is a tuple instead of a Result.

Reporting the bytes transferred while in progress would actually be most useful for us as well but may add some complexity

zephyrchien commented 1 year ago

Merged, just to follow std & tokio. The overhead is quite small here.

howardjohn commented 1 year ago

Thanks!