Move Cow/Convert out of curves module.

This commit is contained in:
Sean Bowe 2017-05-07 09:39:01 -06:00
parent 72a386ec34
commit a98e84e09a
2 changed files with 39 additions and 36 deletions

View File

@ -1,11 +1,12 @@
use rand;
use std::fmt;
use std::ops::Deref;
use std::borrow::Borrow;
use std::marker::PhantomData;
use serde::{Serialize, Deserialize};
use super::{Cow, Convert};
pub mod bls381;
pub trait Engine: Sized + Clone
@ -199,11 +200,6 @@ pub trait SnarkField<E: Engine>: PrimeField<E>
fn root_of_unity(&E) -> Self;
}
pub struct BitIterator<T> {
t: T,
n: usize
}
pub struct WindowTable<E, G, Table: Borrow<[G]>> {
table: Table,
wnaf: Vec<i64>,
@ -248,6 +244,11 @@ impl<E: Engine, G: Group<E>> WindowTable<E, G, Vec<G>> {
}
}
pub struct BitIterator<T> {
t: T,
n: usize
}
impl<T: AsRef<[u64]>> Iterator for BitIterator<T> {
type Item = bool;
@ -276,36 +277,6 @@ impl<'a> From<&'a [u64]> for BitIterator<&'a [u64]>
}
}
pub enum Cow<'a, T: 'a> {
Owned(T),
Borrowed(&'a T)
}
impl<'a, T: 'a> Deref for Cow<'a, T> {
type Target = T;
fn deref(&self) -> &T {
match *self {
Cow::Owned(ref v) => v,
Cow::Borrowed(v) => v
}
}
}
pub trait Convert<T: ?Sized, E> {
type Target: Borrow<T>;
fn convert(&self, &E) -> Cow<Self::Target>;
}
impl<T, E> Convert<T, E> for T {
type Target = T;
fn convert(&self, _: &E) -> Cow<T> {
Cow::Borrowed(self)
}
}
macro_rules! bit_iter_impl(
($n:expr) => {
impl From<[u64; $n]> for BitIterator<[u64; $n]> {

View File

@ -11,6 +11,8 @@ pub mod groth16;
use std::collections::HashMap;
use std::ops;
use std::ops::Deref;
use std::borrow::Borrow;
use curves::{Engine, Field};
@ -149,3 +151,33 @@ pub trait ConstraintSystem<E: Engine> {
c: LinearCombination<E>
);
}
pub enum Cow<'a, T: 'a> {
Owned(T),
Borrowed(&'a T)
}
impl<'a, T: 'a> Deref for Cow<'a, T> {
type Target = T;
fn deref(&self) -> &T {
match *self {
Cow::Owned(ref v) => v,
Cow::Borrowed(v) => v
}
}
}
pub trait Convert<T: ?Sized, E> {
type Target: Borrow<T>;
fn convert(&self, &E) -> Cow<Self::Target>;
}
impl<T, E> Convert<T, E> for T {
type Target = T;
fn convert(&self, _: &E) -> Cow<T> {
Cow::Borrowed(self)
}
}