Skip to content

Commit

Permalink
fix: add Copy trait bound to struct
Browse files Browse the repository at this point in the history
  • Loading branch information
vmx committed Mar 30, 2020
1 parent 8090040 commit 27d716d
Showing 1 changed file with 18 additions and 30 deletions.
48 changes: 18 additions & 30 deletions src/cid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ pub type Cid = CidGeneric<Code, Codec>;
#[derive(PartialEq, Eq, Clone, Debug, PartialOrd, Ord)]
pub struct CidGeneric<T, U>
where
T: Into<u64> + TryFrom<u64>,
U: Into<u64> + TryFrom<u64>,
T: Into<u64> + TryFrom<u64> + Copy,
U: Into<u64> + TryFrom<u64> + Copy,
{
/// The version of CID.
version: Version,
Expand All @@ -30,9 +30,9 @@ where

impl<T, U> CidGeneric<T, U>
where
T: Into<u64> + TryFrom<u64>,
T: Into<u64> + TryFrom<u64> + Copy,
<T as TryFrom<u64>>::Error: std::fmt::Debug,
U: Into<u64> + TryFrom<u64>,
U: Into<u64> + TryFrom<u64> + Copy,
<U as TryFrom<u64>>::Error: std::fmt::Debug,
{
/// Create a new CIDv0.
Expand Down Expand Up @@ -76,10 +76,7 @@ where
}

/// Returns the cid codec.
pub fn codec(&self) -> U
where
U: Copy,
{
pub fn codec(&self) -> U {
self.codec
}

Expand All @@ -92,21 +89,15 @@ where
Base::Base58Btc.encode(self.hash.as_bytes())
}

fn to_string_v1(&self) -> String
where
U: Copy,
{
fn to_string_v1(&self) -> String {
multibase::encode(Base::Base32Lower, self.to_bytes().as_slice())
}

fn to_bytes_v0(&self) -> Vec<u8> {
self.hash.to_vec()
}

fn to_bytes_v1(&self) -> Vec<u8>
where
U: Copy,
{
fn to_bytes_v1(&self) -> Vec<u8> {
let mut res = Vec::with_capacity(16);

let mut buf = varint_encode::u64_buffer();
Expand All @@ -121,10 +112,7 @@ where
}

/// Convert CID to encoded bytes.
pub fn to_bytes(&self) -> Vec<u8>
where
U: Copy,
{
pub fn to_bytes(&self) -> Vec<u8> {
match self.version {
Version::V0 => self.to_bytes_v0(),
Version::V1 => self.to_bytes_v1(),
Expand Down Expand Up @@ -163,9 +151,9 @@ where

impl<T, U> std::str::FromStr for CidGeneric<T, U>
where
T: Into<u64> + TryFrom<u64>,
T: Into<u64> + TryFrom<u64> + Copy,
<T as TryFrom<u64>>::Error: std::fmt::Debug,
U: Into<u64> + TryFrom<u64>,
U: Into<u64> + TryFrom<u64> + Copy,
<U as TryFrom<u64>>::Error: std::fmt::Debug,
{
type Err = Error;
Expand All @@ -177,9 +165,9 @@ where

impl<T, U> TryFrom<String> for CidGeneric<T, U>
where
T: Into<u64> + TryFrom<u64>,
T: Into<u64> + TryFrom<u64> + Copy,
<T as TryFrom<u64>>::Error: std::fmt::Debug,
U: Into<u64> + TryFrom<u64>,
U: Into<u64> + TryFrom<u64> + Copy,
<U as TryFrom<u64>>::Error: std::fmt::Debug,
{
type Error = Error;
Expand All @@ -191,9 +179,9 @@ where

impl<T, U> TryFrom<&str> for CidGeneric<T, U>
where
T: Into<u64> + TryFrom<u64>,
T: Into<u64> + TryFrom<u64> + Copy,
<T as TryFrom<u64>>::Error: std::fmt::Debug,
U: Into<u64> + TryFrom<u64>,
U: Into<u64> + TryFrom<u64> + Copy,
<U as TryFrom<u64>>::Error: std::fmt::Debug,
{
type Error = Error;
Expand Down Expand Up @@ -223,9 +211,9 @@ where

impl<T, U> TryFrom<Vec<u8>> for CidGeneric<T, U>
where
T: Into<u64> + TryFrom<u64>,
T: Into<u64> + TryFrom<u64> + Copy,
<T as TryFrom<u64>>::Error: std::fmt::Debug,
U: Into<u64> + TryFrom<u64>,
U: Into<u64> + TryFrom<u64> + Copy,
<U as TryFrom<u64>>::Error: std::fmt::Debug,
{
type Error = Error;
Expand All @@ -237,9 +225,9 @@ where

impl<T, U> TryFrom<&[u8]> for CidGeneric<T, U>
where
T: Into<u64> + TryFrom<u64>,
T: Into<u64> + TryFrom<u64> + Copy,
<T as TryFrom<u64>>::Error: std::fmt::Debug,
U: Into<u64> + TryFrom<u64>,
U: Into<u64> + TryFrom<u64> + Copy,
<U as TryFrom<u64>>::Error: std::fmt::Debug,
{
type Error = Error;
Expand Down

0 comments on commit 27d716d

Please sign in to comment.