Browse Source

Move Cow/Convert out of curves module.

master
Sean Bowe 7 years ago
parent
commit
a98e84e09a
  1. 43
      src/curves/mod.rs
  2. 32
      src/lib.rs

43
src/curves/mod.rs

@ -1,11 +1,12 @@
use rand; use rand;
use std::fmt; use std::fmt;
use std::ops::Deref;
use std::borrow::Borrow; use std::borrow::Borrow;
use std::marker::PhantomData; use std::marker::PhantomData;
use serde::{Serialize, Deserialize}; use serde::{Serialize, Deserialize};
use super::{Cow, Convert};
pub mod bls381; pub mod bls381;
pub trait Engine: Sized + Clone pub trait Engine: Sized + Clone
@ -199,11 +200,6 @@ pub trait SnarkField<E: Engine>: PrimeField<E>
fn root_of_unity(&E) -> Self; fn root_of_unity(&E) -> Self;
} }
pub struct BitIterator<T> {
t: T,
n: usize
}
pub struct WindowTable<E, G, Table: Borrow<[G]>> { pub struct WindowTable<E, G, Table: Borrow<[G]>> {
table: Table, table: Table,
wnaf: Vec<i64>, 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> { impl<T: AsRef<[u64]>> Iterator for BitIterator<T> {
type Item = bool; 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( macro_rules! bit_iter_impl(
($n:expr) => { ($n:expr) => {
impl From<[u64; $n]> for BitIterator<[u64; $n]> { impl From<[u64; $n]> for BitIterator<[u64; $n]> {

32
src/lib.rs

@ -11,6 +11,8 @@ pub mod groth16;
use std::collections::HashMap; use std::collections::HashMap;
use std::ops; use std::ops;
use std::ops::Deref;
use std::borrow::Borrow;
use curves::{Engine, Field}; use curves::{Engine, Field};
@ -149,3 +151,33 @@ pub trait ConstraintSystem<E: Engine> {
c: LinearCombination<E> 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)
}
}

Loading…
Cancel
Save