Add specific methods for getting at UserData

This commit is contained in:
Emii Tatsuo 2020-11-28 15:01:39 -05:00
parent e7cbbd7d91
commit 4a2293ddc6
Signed by: Emi
GPG Key ID: 68FAB2E2E6DFC98B
1 changed files with 19 additions and 5 deletions

View File

@ -375,6 +375,23 @@ impl<UserData: Serialize + DeserializeOwned> RegisteredUser<UserData> {
pub fn has_password(&self) -> bool {
self.inner.pass_hash.is_some()
}
/// Get an immutable reference to the data associated with this user
pub fn data(&self) -> &UserData {
&self.inner.data
}
/// Get a mutable reference to the data associated with this user
///
/// This automatically flags the user data as needing to be saved to the database,
/// which automatically performs the action when this user falls out of scope. If
/// need be, you can push these changes to the database sooner by calling [`save()`]
///
/// [`save()`]: Self::save()
pub fn mut_data(&mut self) -> &mut UserData {
self.has_changed = true;
&mut self.inner.data
}
}
impl<UserData: Serialize + DeserializeOwned> std::ops::Drop for RegisteredUser<UserData> {
@ -389,15 +406,12 @@ impl<UserData: Serialize + DeserializeOwned> std::ops::Drop for RegisteredUser<U
impl<UserData: Serialize + DeserializeOwned> AsRef<UserData> for RegisteredUser<UserData> {
fn as_ref(&self) -> &UserData {
&self.inner.data
self.data()
}
}
impl<UserData: Serialize + DeserializeOwned> AsMut<UserData> for RegisteredUser<UserData> {
/// NOTE: Changes made to the user data won't be persisted until RegisteredUser::save
/// is called
fn as_mut(&mut self) -> &mut UserData {
self.has_changed = true;
&mut self.inner.data
self.mut_data()
}
}