Node

Struct Node 

Source
pub struct Node {
    pub id: u64,
    /* private fields */
}
Expand description

A node holds multiple Worker structures and has a, ideally, unique ID given.

Fields§

§id: u64

The ID of the node, ideally it should be unique and not be used multiple times within an application.

Implementations§

Source§

impl Node

The default implementation of a node.

Source

pub fn new(id: u64) -> Self

Create a new node for the given ID.

Examples found in repository?
examples/basic.rs (line 4)
3fn main() {
4    let mut node = spaceflake::Node::new(1);
5    let worker = node.new_worker();
6    let sf = worker.generate();
7    match sf {
8        Ok(value) => {
9            println!("Generated Spaceflake: {:#?}", value.decompose())
10        }
11        Err(error) => {
12            println!("Error: {}", error)
13        }
14    }
15}
More examples
Hide additional examples
examples/bulk.rs (line 16)
3fn main() {
4    let settings = spaceflake::BulkGeneratorSettings::new(2_000_000);
5    let mut spaceflakes = spaceflake::bulk_generate(settings);
6    match spaceflakes {
7        Ok(value) => {
8            println!("Successfully generated {} Spaceflakes", value.len());
9            println!("{:#?}", value[1337331].decompose());
10        }
11        Err(error) => {
12            println!("Error: {}", error)
13        }
14    }
15
16    let node_one = spaceflake::Node::new(1);
17    spaceflakes = node_one.bulk_generate(1_000_000);
18    match spaceflakes {
19        Ok(value) => {
20            println!("Successfully generated {} Spaceflakes", value.len());
21            println!("{:#?}", value[7331].decompose());
22        }
23        Err(error) => {
24            println!("Error: {}", error)
25        }
26    }
27
28    let mut node_two = spaceflake::Node::new(2);
29    let worker = node_two.new_worker();
30    spaceflakes = worker.bulk_generate(500_000);
31    match spaceflakes {
32        Ok(value) => {
33            println!("Successfully generated {} Spaceflakes", value.len());
34            println!("{:#?}", value[1337].decompose());
35        }
36        Err(error) => {
37            println!("Error: {}", error)
38        }
39    }
40}
Source

pub fn new_worker(&mut self) -> Worker

Create a new worker and push it to the list of workers of the node to generate Spaceflakes.

Examples found in repository?
examples/basic.rs (line 5)
3fn main() {
4    let mut node = spaceflake::Node::new(1);
5    let worker = node.new_worker();
6    let sf = worker.generate();
7    match sf {
8        Ok(value) => {
9            println!("Generated Spaceflake: {:#?}", value.decompose())
10        }
11        Err(error) => {
12            println!("Error: {}", error)
13        }
14    }
15}
More examples
Hide additional examples
examples/bulk.rs (line 29)
3fn main() {
4    let settings = spaceflake::BulkGeneratorSettings::new(2_000_000);
5    let mut spaceflakes = spaceflake::bulk_generate(settings);
6    match spaceflakes {
7        Ok(value) => {
8            println!("Successfully generated {} Spaceflakes", value.len());
9            println!("{:#?}", value[1337331].decompose());
10        }
11        Err(error) => {
12            println!("Error: {}", error)
13        }
14    }
15
16    let node_one = spaceflake::Node::new(1);
17    spaceflakes = node_one.bulk_generate(1_000_000);
18    match spaceflakes {
19        Ok(value) => {
20            println!("Successfully generated {} Spaceflakes", value.len());
21            println!("{:#?}", value[7331].decompose());
22        }
23        Err(error) => {
24            println!("Error: {}", error)
25        }
26    }
27
28    let mut node_two = spaceflake::Node::new(2);
29    let worker = node_two.new_worker();
30    spaceflakes = worker.bulk_generate(500_000);
31    match spaceflakes {
32        Ok(value) => {
33            println!("Successfully generated {} Spaceflakes", value.len());
34            println!("{:#?}", value[1337].decompose());
35        }
36        Err(error) => {
37            println!("Error: {}", error)
38        }
39    }
40}
Source

pub fn remove_worker(&mut self, id: u64)

Remove a worker given its ID from the list of workers.

Source

pub fn get_workers(&self) -> Vec<Worker>

Returns the list of workers the node is currently holding.

Source

pub fn bulk_generate(&self, amount: usize) -> Result<Vec<Spaceflake>, String>

Generate an amount of Spaceflakes on the node.

The workers will automatically scale, so there is no need to add new workers to the node.

Examples found in repository?
examples/bulk.rs (line 17)
3fn main() {
4    let settings = spaceflake::BulkGeneratorSettings::new(2_000_000);
5    let mut spaceflakes = spaceflake::bulk_generate(settings);
6    match spaceflakes {
7        Ok(value) => {
8            println!("Successfully generated {} Spaceflakes", value.len());
9            println!("{:#?}", value[1337331].decompose());
10        }
11        Err(error) => {
12            println!("Error: {}", error)
13        }
14    }
15
16    let node_one = spaceflake::Node::new(1);
17    spaceflakes = node_one.bulk_generate(1_000_000);
18    match spaceflakes {
19        Ok(value) => {
20            println!("Successfully generated {} Spaceflakes", value.len());
21            println!("{:#?}", value[7331].decompose());
22        }
23        Err(error) => {
24            println!("Error: {}", error)
25        }
26    }
27
28    let mut node_two = spaceflake::Node::new(2);
29    let worker = node_two.new_worker();
30    spaceflakes = worker.bulk_generate(500_000);
31    match spaceflakes {
32        Ok(value) => {
33            println!("Successfully generated {} Spaceflakes", value.len());
34            println!("{:#?}", value[1337].decompose());
35        }
36        Err(error) => {
37            println!("Error: {}", error)
38        }
39    }
40}

Trait Implementations§

Source§

impl Debug for Node

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Node

§

impl RefUnwindSafe for Node

§

impl Send for Node

§

impl Sync for Node

§

impl Unpin for Node

§

impl UnwindSafe for Node

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V