scalar_field/
fields.rs

1use crate::ScalarField;
2use core::marker::PhantomData;
3
4#[derive(Copy, Clone, Debug)]
5pub struct ScalarFieldWrapper<T>(T);
6
7impl<T: ScalarField> From<T> for ScalarFieldWrapper<T> {
8	fn from(inner: T) -> Self {
9		Self(inner)
10	}
11}
12
13pub fn wrap<T: ScalarField>(inner: T) -> ScalarFieldWrapper<T> {
14	ScalarFieldWrapper::from(inner)
15}
16
17impl<T: ScalarField> ScalarField for ScalarFieldWrapper<T> {
18	type Point = T::Point;
19	type Scalar = T::Scalar;
20
21	fn value(&self, point: Self::Point) -> Self::Scalar {
22		self.0.value(point)
23	}
24}
25
26#[derive(Copy, Clone, Debug)]
27pub struct ConstantField<P, S>(S, PhantomData<P>);
28
29impl<P, S: Clone> ScalarField for ConstantField<P, S> {
30	type Point = P;
31	type Scalar = S;
32
33	fn value(&self, _: Self::Point) -> Self::Scalar {
34		self.0.clone()
35	}
36}
37
38pub fn constant<P, S: Clone>(inner: S) -> ConstantField<P, S> {
39	ConstantField(inner, PhantomData)
40}
41
42#[derive(Copy, Clone, Debug)]
43pub struct FunctionField<P, F>(F, PhantomData<P>);
44
45impl<P, S, F> ScalarField for FunctionField<P, F>
46where
47	F: Fn(P) -> S,
48{
49	type Point = P;
50	type Scalar = S;
51
52	fn value(&self, point: Self::Point) -> Self::Scalar {
53		self.0(point)
54	}
55}
56
57pub fn function<P, S, F: Fn(P) -> S>(func: F) -> FunctionField<P, F> {
58	FunctionField(func, PhantomData)
59}
60
61#[derive(Copy, Clone, Debug)]
62pub struct IdentityField<T>(PhantomData<T>);
63
64impl<T> ScalarField for IdentityField<T> {
65	type Point = T;
66	type Scalar = T;
67
68	fn value(&self, point: Self::Point) -> Self::Scalar {
69		point
70	}
71}
72
73pub fn identity<T>() -> IdentityField<T> {
74	IdentityField(PhantomData)
75}