revisionstore: make ContentStore::sha256 infallible

Summary:
Instead of using Sha256::from_slice, just use Sha256::from with a correctly
sized array.

Reviewed By: quark-zju

Differential Revision: D20756181

fbshipit-source-id: 17c869325025078e4c91a564fc57ac1d9345dd15
This commit is contained in:
Xavier Deguillard 2020-03-31 08:08:41 -07:00 committed by Facebook GitHub Bot
parent 1e18df1681
commit 2ce3c6784b
4 changed files with 20 additions and 15 deletions

View File

@ -725,7 +725,7 @@ mod tests {
let k1 = key("a", "2");
let data = Bytes::from(&[1, 2, 3, 4, 5][..]);
let hash = ContentHash::sha256(&data)?;
let hash = ContentHash::sha256(&data);
let delta = Delta {
data,
base: None,

View File

@ -305,7 +305,7 @@ impl LfsIndexedLogBlobsStore {
}
let data = res.freeze();
if &ContentHash::sha256(&data)?.unwrap_sha256() != hash {
if &ContentHash::sha256(&data).unwrap_sha256() != hash {
Ok(None)
} else {
Ok(Some(data))
@ -406,7 +406,7 @@ impl LfsBlobsStore {
let mut buf = Vec::new();
file.read_to_end(&mut buf)?;
let blob = Bytes::from(buf);
if &ContentHash::sha256(&blob)?.unwrap_sha256() != hash {
if &ContentHash::sha256(&blob).unwrap_sha256() != hash {
None
} else {
Some(blob)
@ -641,7 +641,7 @@ impl HgIdMutableDeltaStore for LfsStore {
ensure!(delta.base.is_none(), "Deltas aren't supported.");
let (data, copy_from) = strip_metadata(&delta.data)?;
let content_hash = ContentHash::sha256(&data)?;
let content_hash = ContentHash::sha256(&data);
match content_hash {
ContentHash::Sha256(sha256) => self.blobs.add(&sha256, data.clone())?,
@ -1229,7 +1229,7 @@ mod tests {
store.flush()?;
let indexedlog_blobs = LfsIndexedLogBlobsStore::shared(&dir.path(), &config)?;
let hash = ContentHash::sha256(&delta.data)?.unwrap_sha256();
let hash = ContentHash::sha256(&delta.data).unwrap_sha256();
assert!(indexedlog_blobs.contains(&hash)?);
@ -1246,7 +1246,7 @@ mod tests {
let loose_store = LfsBlobsStore::Loose(get_lfs_objects_path(dir.path())?, false);
let data = Bytes::from(&[1, 2, 3, 4][..]);
let sha256 = ContentHash::sha256(&data)?.unwrap_sha256();
let sha256 = ContentHash::sha256(&data).unwrap_sha256();
loose_store.add(&sha256, data.clone())?;
assert!(blob_store.contains(&sha256)?);
@ -1334,7 +1334,7 @@ mod tests {
let data = Bytes::from(&[1, 2, 3, 4][..]);
let partial = data.slice(2..);
let sha256 = ContentHash::sha256(&data)?.unwrap_sha256();
let sha256 = ContentHash::sha256(&data).unwrap_sha256();
let entry = LfsIndexedLogBlobsEntry {
sha256: sha256.clone(),
@ -1358,7 +1358,7 @@ mod tests {
let store = LfsIndexedLogBlobsStore::shared(dir.path(), &config)?;
let data = Bytes::from(&[1, 2, 3, 4, 5, 6, 7][..]);
let sha256 = ContentHash::sha256(&data)?.unwrap_sha256();
let sha256 = ContentHash::sha256(&data).unwrap_sha256();
let first = data.slice(0..1);
let second = data.slice(1..4);
@ -1400,7 +1400,7 @@ mod tests {
let store = LfsIndexedLogBlobsStore::shared(dir.path(), &config)?;
let data = Bytes::from(&[1, 2, 3, 4, 5, 6, 7][..]);
let sha256 = ContentHash::sha256(&data)?.unwrap_sha256();
let sha256 = ContentHash::sha256(&data).unwrap_sha256();
let first = data.slice(0..4);
let second = data.slice(2..3);
@ -1439,7 +1439,7 @@ mod tests {
let data = Bytes::from(data);
let mut content_hashes = HashMap::new();
content_hashes.insert(ContentHashType::Sha256, ContentHash::sha256(&data)?);
content_hashes.insert(ContentHashType::Sha256, ContentHash::sha256(&data));
let pointer = LfsPointersEntry {
hgid: hgid("1234"),
@ -1674,7 +1674,7 @@ mod tests {
let indexedlog = Arc::new(IndexedLogHgIdDataStore::new(&dir)?);
let blob = Bytes::from(&b"\x01\nTHIS IS A BLOB WITH A HEADER"[..]);
let sha256 = match ContentHash::sha256(&blob)? {
let sha256 = match ContentHash::sha256(&blob) {
ContentHash::Sha256(sha256) => sha256,
};
let size = blob.len();
@ -1886,7 +1886,7 @@ mod tests {
let k1 = key("a", "2");
let data = Bytes::from(&[1, 2, 3, 4][..]);
let hash = ContentHash::sha256(&data)?;
let hash = ContentHash::sha256(&data);
let delta = Delta {
data,
base: None,

View File

@ -5,7 +5,6 @@
* GNU General Public License version 2.
*/
use anyhow::Result;
use bytes::Bytes;
use serde_derive::{Deserialize, Serialize};
use sha2::Digest;
@ -41,12 +40,12 @@ pub enum StoreKey {
}
impl ContentHash {
pub fn sha256(data: &Bytes) -> Result<Self> {
pub fn sha256(data: &Bytes) -> Self {
let mut hash = sha2::Sha256::new();
hash.input(data);
let bytes: [u8; Sha256::len()] = hash.result().into();
Ok(ContentHash::Sha256(Sha256::from_slice(&bytes)?))
ContentHash::Sha256(Sha256::from(bytes))
}
pub fn unwrap_sha256(self) -> Sha256 {

View File

@ -74,6 +74,12 @@ impl<'a> From<&'a [u8; Sha256::len()]> for Sha256 {
}
}
impl From<[u8; Sha256::len()]> for Sha256 {
fn from(bytes: [u8; Sha256::len()]) -> Sha256 {
Sha256(bytes)
}
}
impl AsRef<[u8]> for Sha256 {
fn as_ref(&self) -> &[u8] {
&self.0