From ccb9d16e5dbe965e5a604e1cb402cd3bc7de0df5 Mon Sep 17 00:00:00 2001 From: Austin Hellyer Date: Mon, 5 Dec 2016 07:46:22 -0800 Subject: Add a ShareMap across contexts The context now exposes, through the Client, a `data` field, which can be accessed safely across contexts. This allows for a custom "shared state" without the need for (ab)using lazy-static.rs. Refer to example 06 for an example on how to use shared data. --- src/client/context.rs | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'src/client/context.rs') diff --git a/src/client/context.rs b/src/client/context.rs index 4fca519..af90447 100644 --- a/src/client/context.rs +++ b/src/client/context.rs @@ -6,6 +6,7 @@ use std::sync::{Arc, Mutex}; use super::gateway::Shard; use super::rest::{self, GuildPagination}; use super::login_type::LoginType; +use typemap::ShareMap; use ::utils::builder::{ CreateEmbed, CreateInvite, @@ -82,6 +83,11 @@ pub struct Context { /// /// [`on_message`]: struct.Client.html#method.on_message pub channel_id: Option, + /// A clone of [`Client::data`]. Refer to its documentation for more + /// information. + /// + /// [`Client::data`]: struct.Client.html#method.data + pub data: Arc>, /// The associated shard which dispatched the event handler. /// /// Note that if you are sharding, in relevant terms, this is the shard @@ -101,9 +107,11 @@ impl Context { #[doc(hidden)] pub fn new(channel_id: Option, shard: Arc>, + data: Arc>, login_type: LoginType) -> Context { Context { channel_id: channel_id, + data: data, shard: shard, login_type: login_type, } -- cgit v1.2.3