Skip to content

Please Implement Clone for PaddingScheme #130

@icf3ver

Description

@icf3ver

Please implement the recommended traits for the enum PaddingScheme.

Clone is missing from PaddingScheme. Please fix this.

While copy may not be possible due to problems when copying RngCore--namely that an accidental copy may generate the same numbers--, clone is an important implementation which is advised by the creators of rand. With the added implementation of box-clone on Box<dyn DynDigest> and on Box<dyn RngCore>, written out in full bellow, it should be possible to derive Clone on PaddingScheme.

for DynDigest:

pub trait DynDigest {
    ...
}

pub trait DynDigestBoxClone {
    fn clone_dyn_digest(&self) -> Box<dyn DynDigest>;
}

impl<T> DynDigestBoxClone for T where T: 'static + DynDigest + Clone {
    fn clone_dyn_digest(&self) -> Box<dyn DynDigest> {
        Box::new(self.clone())
    }
}

impl Clone for Box<dyn DynDigest> {
    fn clone(&self) -> Self {
        self.clone_dyn_digest()
    }
}

and the same for Box<dyn RngCore>:

use rand::RngCore;

pub trait RngCoreClone: RngCore {
    fn clone_rng_core(&self) -> Box<dyn RngCoreClone>;
}

impl<T: 'static + RngCore + Clone> RngCoreClone for T {
    fn clone_rng_core(&self) -> Box<dyn RngCoreClone> {
        Box::new(self.clone())
    }
}

impl Clone for Box<dyn RngCoreClone> {
    fn clone(&self) -> Box<dyn RngCoreClone> {
        self.clone_rng_core()
    }
}

and switch to using Box<dyn RngCoreClone> rather than Box<dyn RngCore>.

Thanks.

Best regards,
littleTitan

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions