Rename 'or_default' to 'extract'. Better meaning.

This commit is contained in:
projectmoon 2021-01-10 20:54:05 +00:00
parent efca366f3f
commit a36e8069f4
2 changed files with 11 additions and 8 deletions

View File

@ -14,17 +14,20 @@ pub mod api {
//include!(concat!(env!("OUT_DIR"), "/models.proto.cofd.api.rs"));
tonic::include_proto!("models.proto.cofd.api");
/// Trait to extract CharacterIdentifier while avoiding clone. The
/// character identifier is extracted out of the Option, leaving
/// None in its place.
/// Trait to take ownership of CharacterIdentifiers while avoiding
/// clone. The identifier is extracted out of the type, leaving a
/// default value in its place.
pub trait DefaultCharacterIdentifier {
/// Extract the CharacterIdentifier from the containing type,
/// leaving behind a default value.
fn or_default(&mut self) -> CharacterIdentifier;
/// Take ownership of a CharacterIdentifier by taking it out
/// of the containing type, leaving behind a default value
/// (None, in the case of Option) in its place.
fn extract(&mut self) -> CharacterIdentifier;
}
impl DefaultCharacterIdentifier for Option<CharacterIdentifier> {
fn or_default(&mut self) -> CharacterIdentifier {
/// Extract the identifier, or a default one if the identifier
/// is not specified for some reason.
fn extract(&mut self) -> CharacterIdentifier {
self.take().unwrap_or_default()
}
}

View File

@ -44,7 +44,7 @@ pub(super) async fn update_basic_info<'a>(
conn: TenebrousDbConn<'_>,
logged_in_user: Option<&User>,
) -> Result<Proto<ApiResult>, Error> {
let id = req.id.or_default();
let id = req.id.extract();
let mut character = load_character(&conn, logged_in_user, &id.owner, id.character_id).await?;
let mut sheet: CofdSheet = character.try_deserialize()?;