1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use core::hash::{Hash, Hasher};

use wyhash::WyHash;

/// A noise function producing `Value`s.

pub trait Noise: Sized {
	type Value;
	type Unseeded = Self;
}

/// A 'configuration' struct which can be seeded with a `Seed` to create an instance of the type `Seeded`.

pub trait Seedable: Sized {
	type Seed;
	type Seeded: Seeded<Config = Self>;
	
	/// Seed this with `seed` to get an instance of `Seeded`.

	fn seed(self, seed: Self::Seed) -> Self::Seeded;
}

/*
TODO: Make Seedable and Noise mutually exclusive once supported.

impl<T: Seedable> !Noise for T { }
*/

/// A struct which can be created by seeding an instance of `Config`.

pub trait Seeded: Sized {
	type Config: Seedable<Seeded = Self>;
}

/// A domain over which a noise function operates.

pub trait NoiseDomain<Arg>: Noise {
	/// Get the value of the noise at a particular 'location'.

	fn noise(&self, arg: Arg) -> Self::Value;
}

/// A type of seed which can be split into multiple 'child' seeds.

pub trait SplitSeed {
	/// Create a unique child seed.

	fn split(&self, n: usize) -> Self;
}

impl SplitSeed for u64 {
	fn split(&self, n: usize) -> Self {
		let mut hasher = WyHash::with_seed(*self);
		n.hash(&mut hasher);
		hasher.finish()
	}
}