mononoke/blobstore: don't use Buf::bytes() to get a slice

Summary:
This is currently calling `Buf::bytes()` in order to get a `&[u8]` out of
`Bytes`, but this method isn't actually guaranteed to return all data in the
`Buf`: https://docs.rs/bytes/0.5.6/bytes/trait.Buf.html#tymethod.bytes

> Note that this can return shorter slice (this allows non-continuous internal representation)

In practice this is fine because `Bytes` does in fact have a contiguous
internal representation (which is why we can call `as_ref()` on it), but let's
do the more correct thing here.

(I happened to spot this while looking into the feasibility of a Bytes 0.5 ->
Bytes 1.x upgrade, where this `bytes()` method was renamed to `chunk()`)

Reviewed By: farnz

Differential Revision: D28507885

fbshipit-source-id: 73e5f1ba587292f772c702127a3933ea76fceb9f
This commit is contained in:
Thomas Orozco 2021-05-18 10:29:00 -07:00 committed by Facebook GitHub Bot
parent 0bd512d811
commit f5e024f889

View File

@ -16,7 +16,7 @@ use abomonation_derive::Abomonation;
use anyhow::{Error, Result};
use async_trait::async_trait;
use auto_impl::auto_impl;
use bytes::{Buf, Bytes};
use bytes::Bytes;
use context::CoreContext;
use serde_derive::{Deserialize, Serialize};
use std::collections::HashSet;
@ -229,7 +229,7 @@ impl BlobstoreBytes {
let cursor = Cursor::new(bytes);
zstd::decode_all(cursor).map_err(|_| ())?
} else {
bytes.bytes().into()
bytes.as_ref().into()
};
let get_data_serialisable =
@ -257,7 +257,7 @@ struct BlobstoreBytesSerialisable(Vec<u8>);
impl From<BlobstoreBytes> for BlobstoreBytesSerialisable {
fn from(blob_bytes: BlobstoreBytes) -> Self {
BlobstoreBytesSerialisable(blob_bytes.into_bytes().bytes().into())
BlobstoreBytesSerialisable(blob_bytes.into_bytes().as_ref().into())
}
}