ChildSpec

Struct ChildSpec 

Source
pub struct ChildSpec { /* private fields */ }
Expand description

The child specification describes how the supervisor starts, shuts down, and restarts child processes.

Implementations§

Source§

impl ChildSpec

Source

pub fn new<T: Into<String>>(id: T) -> Self

Constructs a new instance of ChildSpec with the given id.

Examples found in repository?
examples/supervisor.rs (line 66)
65    pub fn child_spec(self) -> ChildSpec {
66        ChildSpec::new("MyServer")
67            .start(move || MyServer::start_link(MyServer, GenServerOptions::new()))
68    }
More examples
Hide additional examples
examples/registry.rs (line 72)
58    async fn start(&self) -> Result<Pid, ExitReason> {
59        // Spawn a registry that will take care of registering 'MySpace'.
60        let children = [
61            Registry::new("space-registry")
62                .with_start(|key| {
63                    let RegistryKey::String(id) = key else {
64                        panic!()
65                    };
66
67                    MySpace::new(id).start_link(GenServerOptions::new())
68                })
69                .with_shutdown(Shutdown::Infinity)
70                .child_spec(RegistryOptions::new())
71                .id("space-registry"),
72            ChildSpec::new("test-registry")
73                .start(move || async { Ok(Process::spawn(test_registry())) }),
74        ];
75
76        // Restart only the terminated child.
77        Supervisor::with_children(children)
78            .strategy(SupervisionStrategy::OneForOne)
79            .start_link(SupervisorOptions::new())
80            .await
81    }
Source

pub fn id<T: Into<String>>(self, id: T) -> Self

Sets a new id for this ChildSpec.

Examples found in repository?
examples/supervisor.rs (line 33)
30    async fn start(&self) -> Result<Pid, ExitReason> {
31        // Spawn two instances of `MyServer` with their own unique ids.
32        let children = [
33            MyServer::new().child_spec().id("server1"),
34            MyServer::new().child_spec().id("server2"),
35        ];
36
37        // Restart only the terminated child.
38        Supervisor::with_children(children)
39            .strategy(SupervisionStrategy::OneForOne)
40            .start_link(SupervisorOptions::new())
41            .await
42    }
More examples
Hide additional examples
examples/registry.rs (line 71)
58    async fn start(&self) -> Result<Pid, ExitReason> {
59        // Spawn a registry that will take care of registering 'MySpace'.
60        let children = [
61            Registry::new("space-registry")
62                .with_start(|key| {
63                    let RegistryKey::String(id) = key else {
64                        panic!()
65                    };
66
67                    MySpace::new(id).start_link(GenServerOptions::new())
68                })
69                .with_shutdown(Shutdown::Infinity)
70                .child_spec(RegistryOptions::new())
71                .id("space-registry"),
72            ChildSpec::new("test-registry")
73                .start(move || async { Ok(Process::spawn(test_registry())) }),
74        ];
75
76        // Restart only the terminated child.
77        Supervisor::with_children(children)
78            .strategy(SupervisionStrategy::OneForOne)
79            .start_link(SupervisorOptions::new())
80            .await
81    }
Source

pub fn start<T, F>(self, start: T) -> Self
where T: Fn() -> F + Send + Sync + 'static, F: Future<Output = Result<Pid, ExitReason>> + Send + Sync + 'static,

The method invoked to start the child process.

Must return a future that resolves to Result<Pid, ExitReason>.

Examples found in repository?
examples/supervisor.rs (line 67)
65    pub fn child_spec(self) -> ChildSpec {
66        ChildSpec::new("MyServer")
67            .start(move || MyServer::start_link(MyServer, GenServerOptions::new()))
68    }
More examples
Hide additional examples
examples/registry.rs (line 73)
58    async fn start(&self) -> Result<Pid, ExitReason> {
59        // Spawn a registry that will take care of registering 'MySpace'.
60        let children = [
61            Registry::new("space-registry")
62                .with_start(|key| {
63                    let RegistryKey::String(id) = key else {
64                        panic!()
65                    };
66
67                    MySpace::new(id).start_link(GenServerOptions::new())
68                })
69                .with_shutdown(Shutdown::Infinity)
70                .child_spec(RegistryOptions::new())
71                .id("space-registry"),
72            ChildSpec::new("test-registry")
73                .start(move || async { Ok(Process::spawn(test_registry())) }),
74        ];
75
76        // Restart only the terminated child.
77        Supervisor::with_children(children)
78            .strategy(SupervisionStrategy::OneForOne)
79            .start_link(SupervisorOptions::new())
80            .await
81    }
Source

pub const fn restart(self, restart: Restart) -> Self

Defines when a terminated child process should be restarted.

Defaults to Restart::Permanent.

Source

pub fn shutdown<T: Into<Shutdown>>(self, shutdown: T) -> Self

Defines how a process should be terminated, specifically how long to wait before forcefully killing it.

Defaults to 5s if the type is worker or infinity if the type is supervisor.

Source

pub const fn child_type(self, child_type: ChildType) -> Self

Specifies the type of child process.

Source

pub const fn significant(self, significant: bool) -> Self

A boolean indicating if the child process should be considered significant with regard to automatic shutdown.

Only transient and temporary child processes can be marked as significant.

Defaults to false.

Trait Implementations§

Source§

impl Clone for ChildSpec

Source§

fn clone(&self) -> ChildSpec

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more