-
Notifications
You must be signed in to change notification settings - Fork 18k
io: add SectionReader.Outer method #61870
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
Comments
I think we want to change the parameter names to NewSectionReader:
And then add two new methods Base() int64 and Size() int64. Do I have that right? |
This proposal has been added to the active column of the proposals project |
@rsc using exported methods is definitely a valid alternative as well (see also the other ones), although with a small note:
The two methods that would be required are To summarize, the alternate minimal method-based proposal would be: func NewSectionReader(r ReaderAt, base, size int64) *SectionReader { /* ... */ }
func (r *SectionReader) Parent() ReaderAt { return r.r }
func (r *SectionReader) Base() int64 { return r.base } and, if we instead want to enable fetching all offsets without modifying the current offset with func (r *SectionReader) Offset() int64 { return r.off-r.base } |
Maybe func (s *SectionReader) ReaderAt() ReaderAt { return s.r } instead of Parent()? |
While I'm open to consider other names, SectionReader itself Is a ReaderAt... so calling the method |
It sounds like maybe just exporting the fields would be easiest. We'd want to rename the parameter to NewSectionReader from 'off, n' to 'base, size' (keeping 'n'), and then
The current s.limit would be s.Base+s.Size, so some other code will need to be adjusted (there should be no unexported fields). Do I have that right? |
Ouch. I guess we could call it |
Not a huge fan of having two ways ( |
Yeah, it's also not great to have a mix of exported and unexported fields. |
Just to sumamrize where we are: Current statefunc NewSectionReader(r ReaderAt, off, n int64) *SectionReader
type SectionReader struct {}
func (s *SectionReader) Read(p []byte) (n int, err error)
func (s *SectionReader) Seek(offset int64, whence int) (int64, error)
func (s *SectionReader) ReadAt(p []byte, off int64) (n int, err error)
func (s *SectionReader) Size() int64 The two options below satisfy these criteria:
Option 1 - Expose the fieldsfunc NewSectionReader(r ReaderAt, base, n int64) *SectionReader
type SectionReader struct {
R ReaderAt // NEW
Base int64 // NEW
Off int64 // NEW (~ Seek(0, SeekCurrent))
N int64 // NEW (~ Size() or Seek(0, SeekEnd))
}
func (s *SectionReader) Read(p []byte) (n int, err error)
func (s *SectionReader) Seek(offset int64, whence int) (int64, error)
func (s *SectionReader) ReadAt(p []byte, off int64) (n int, err error)
func (s *SectionReader) Size() int64
Option 2 - Expose methodsfunc NewSectionReader(parent ReaderAt, base, size int64) *SectionReader
type SectionReader struct {}
func (s *SectionReader) Read(p []byte) (n int, err error)
func (s *SectionReader) Seek(offset int64, whence int) (int64, error)
func (s *SectionReader) ReadAt(p []byte, off int64) (n int, err error)
func (s *SectionReader) Size() int64 // (~ Seek(0, SeekEnd))
func (s *SectionReader) Base() int64 // NEW
func (s *SectionReader) Parent() ReaderAt // NEW
|
Parent seems odd since this is not a tree. Outer seems fine. And what if it returns the exact arguments that were passed to NewSectionReader, so that NewSectionReader(sr.Outer()) gets you exactly the same section reader (except for having an independent seek offset when using Read).
|
SGTM |
Based on the discussion above, this proposal seems like a likely accept. |
Change https://go.dev/cl/526855 mentions this issue: |
Fixes: golang#61870 Updates: golang#61727 Change-Id: Iaef9b59c402d68f6bf64be212db2b6746abe8900
Fixes: golang#61870 Updates: golang#61727 Change-Id: Iaef9b59c402d68f6bf64be212db2b6746abe8900
No change in consensus, so accepted. 🎉 |
Fixes golang#61870 Updates golang#61727 Change-Id: Iaef9b59c402d68f6bf64be212db2b6746abe8900 Reviewed-on: https://go-review.googlesource.com/c/go/+/526855 Reviewed-by: Ian Lance Taylor <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> Auto-Submit: Dmitri Shuralyov <[email protected]>
Update, Sep 6: The current proposal is #61870 (comment)
To support
sendfile
onSectionReader
I propose exposing ther
andbase
fields ofSectionReader
, that would thus become:The other two fields (
off
andlimit
) do not need to be exposed (at least for the use-case above) because they can be obtained (albeit in a slightly round-about way) usingSeek
.Exposing
Base
as described above will likely require some adjustments to the internal logic to ensure thatBase
is valid every time it is used internally bySectionReader
(as it may have been modified between calls). Changing theBase
would not affect the absolute position of the end of the section (i.e. changing theBase
would change the length of the section, not its end position). Similarly, it would not affect the absolute position of the current read position, apart from the case in which changing theBase
to be past the current read position would cause theRead
to start returningerrOffset
until a subsequentSeek
operation moves the read position to a valid one (or theBase
is changed again to an offset not greater than the read position). Given the additional foot guns, we may want to add a comment discouraging users from manually changingBase
and instead constructing a newSectionReader
.It is worth noting that enabling the mutability of both fields is not a goal of this proposal, merely a side effect of the design. As an alternative,
Base() int64
could be aSectionReader
method returningr.base
- something that would avoid the foot guns mentioned above.The text was updated successfully, but these errors were encountered: