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

Add Blob::size() #462

Merged
merged 4 commits into from
Sep 6, 2019
Merged

Add Blob::size() #462

merged 4 commits into from
Sep 6, 2019

Conversation

ksqsf
Copy link
Contributor

@ksqsf ksqsf commented Aug 29, 2019

I hope this is trivial enough. :-)

@@ -34,6 +34,11 @@ impl<'repo> Blob<'repo> {
}
}

/// Get the size of this blob.
pub fn size(&self) -> usize {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a reason to choose usize instead of git_off_t? I think (although unlikely) this will silently truncate on 32-bit systems.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed, it should be a 64-bit integer type, but I'm quite confused which type should be used here. git2 doesn't seem to return any value of a "raw" type (like git_off_t) so I picked u64, as a size should be an unsigned integer.

src/blob.rs Outdated Show resolved Hide resolved
@alexcrichton
Copy link
Member

Hm so actually, I might disagree with the usize -> u64 transition because I think a Blob is guaranteed to be in-memory, which guarantees that the size fits in usize. I'm not sure if git can handle >4GB blobs maybe?

@alexcrichton
Copy link
Member

(or maybe this is an API bug in libgit2 by exposing the ability to load any blob in-memory)

@ksqsf
Copy link
Contributor Author

ksqsf commented Sep 4, 2019

If I understand the code of libgit2 correctly, libgit2 will read the data (ie. the contents of a blob) into memory, so this indeed looks like a flaw. However, since git by design supports large blobs, returning u64 here should be safe and correct, and libgit2 maintainers might improve this later.

@alexcrichton
Copy link
Member

We already have an API that looks like pub fn content(&self) -> &[u8] so even if libgit2 is changed that would require a breaking change here, so I think it's fine to leave this as usize for now

@ksqsf
Copy link
Contributor Author

ksqsf commented Sep 4, 2019

Fair enough. I've reverted the last commit.

@alexcrichton
Copy link
Member

@ehuss, to confirm, are you ok with the usize change?

@ehuss
Copy link
Contributor

ehuss commented Sep 5, 2019

Oh, yea, seems fine to me!

@alexcrichton alexcrichton merged commit adfd18b into rust-lang:master Sep 6, 2019
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