Skip to main content

HostSide

Enum HostSide 

pub enum HostSide<'a> {
    Server(&'a mut dyn ServerHost),
    Client(&'a mut dyn ClientHost),
}
Expand description

The logical side of a hook’s host.

Variants§

§

Server(&'a mut dyn ServerHost)

The hook fired on the server.

§

Client(&'a mut dyn ClientHost)

The hook fired on the client.

Implementations§

§

impl HostSide<'_>

The logical side on which a hook is executing.

A mutable client hook can use client services:

use mod_api::{self, ClientHost, Hooks, InputKeyEvent};
#[derive(Default)]
struct ScreenMod;
#[mod_api::horizon_mod]
impl Hooks for ScreenMod {
    fn input_key(&self, host: &mut dyn ClientHost, _: &mut InputKeyEvent<'_>) {
        host.mod_screen_open("example");
    }
}

A hook that fires on both sides matches on the two:

use mod_api::HostSide;
fn side(host: HostSide<'_>) -> &'static str {
    match host { HostSide::Server(_) => "server", HostSide::Client(_) => "client" }
}

Wrong-side services do not compile:

ⓘ
use mod_api::Host;
fn wrong(host: &dyn Host) { host.messages().broadcast(todo!()); }
ⓘ
use mod_api::LevelHost;
fn wrong(host: &dyn LevelHost) { host.players().server_players(); }
ⓘ
use mod_api::ClientHost;
fn wrong(host: &mut dyn ClientHost) { host.commands().run_command("list"); }
ⓘ
use mod_api::ServerHost;
fn wrong(host: &mut dyn ServerHost) { host.mod_screen_open("example"); }

A read-only hook receives only the base host, which cannot be used for a server or client operation, or opened as a backend by a mod:

ⓘ
use mod_api::{Host, ServerHost};
fn server(_: &mut dyn ServerHost) {}
fn read(host: &dyn Host) { server(host); }
ⓘ
use mod_api::{ClientHost, Host};
fn client(_: &dyn ClientHost) {}
fn read(host: &dyn Host) { client(host); }
ⓘ
use mod_api::{Host, ServerHost};
fn read(host: &dyn Host) { let _ = host.downcast_ref::<dyn ServerHost>(); }

pub fn reborrow(&mut self) -> HostSide<'_>

Borrows this side’s host again for a shorter call.

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for HostSide<'a>

§

impl<'a> !Send for HostSide<'a>

§

impl<'a> !Sync for HostSide<'a>

§

impl<'a> !UnwindSafe for HostSide<'a>

§

impl<'a> Freeze for HostSide<'a>

§

impl<'a> Unpin for HostSide<'a>

§

impl<'a> UnsafeUnpin for HostSide<'a>

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.