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

io::Seek should implement .tell() #1181

Open
whitequark opened this issue Jun 29, 2015 · 7 comments
Open

io::Seek should implement .tell() #1181

whitequark opened this issue Jun 29, 2015 · 7 comments
Labels
T-libs-api Relevant to the library API team, which will review and decide on the RFC.

Comments

@whitequark
Copy link
Member

A file.tell() is much nicer than file.seek(std::io::SeekFrom::Current(0)). Additionally, I think .tell() doesn't need the file to be mutably borrowed (but I'm not sure).

@liigo
Copy link
Contributor

liigo commented Jul 2, 2015 via email

@thanatos
Copy link

👍; the syntax is so much nicer (and I agree that tell shouldn't need mutability).

Also, I'm wondering about compresseded stream, like gzip: seeking is hard, if not impossible. But there's certainly a position, which is essentially how many bytes have you decompressed thus far.

@nrc nrc added the T-libs-api Relevant to the library API team, which will review and decide on the RFC. label Aug 25, 2016
@ErichDonGubler
Copy link

ErichDonGubler commented Apr 13, 2018

One can extend the current set of Seek implementors with tell() by using this extension trait and blanket impl:

pub trait Tell {
    fn tell(&self) -> std::io::Result<u64>;
}

impl Tell for Seek {
    fn tell(&self) -> std::io::Result<u64> {
        unsafe {
            let s = self as *const Self;
            let s = s as *mut Self;
            (*s).seek(SeekFrom::Current(0))
        }
    }
}

I'm considering making an RFC with the body of the tell implementation above as a sample implementation. Is there a reason the above wouldn't work?

EDIT: Don't do this.

@sfackler
Copy link
Member

It's unsound to create a &mut T from a &T.

@ErichDonGubler
Copy link

ErichDonGubler commented Apr 13, 2018

@sfackler: In general, yes -- in the code above, the idea is that SeekFrom::Current(0) SHOULDN'T change any state. Conceptually, tell really should just be an immutable access, but there IS no immutable API for getting the current file read/write offset. Is there an example you can provide why the sample implementation I provided above wouldn't work? I admit to not having done much research, but my intuition tells me that seek(SeekFrom::Current(0)) should really be a no-op.

EDIT: Don't do this.

@sfackler
Copy link
Member

"should not" is not the same thing as "must not". It is totally possible and fine for code to implement seek right now in a way that mutates internal state.

@whatisaphone
Copy link

Relevant link: the seek_convenience feature implements this as io::Seek::stream_position().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-libs-api Relevant to the library API team, which will review and decide on the RFC.
Projects
None yet
Development

No branches or pull requests

7 participants