From 0e837e308254f3e56b95fcdafa706e40be084bb9 Mon Sep 17 00:00:00 2001 From: Kitsu Date: Sat, 18 Apr 2020 16:33:01 +0300 Subject: [PATCH] Simplify FileHash ctor --- src/debuginfo/line_info.rs | 25 +++++++++---------------- src/debuginfo/mod.rs | 5 ++--- 2 files changed, 11 insertions(+), 19 deletions(-) diff --git a/src/debuginfo/line_info.rs b/src/debuginfo/line_info.rs index afef0b8ec2f6..ae3d39183ba0 100644 --- a/src/debuginfo/line_info.rs +++ b/src/debuginfo/line_info.rs @@ -1,5 +1,4 @@ use std::ffi::OsStr; -use std::convert::TryFrom; use std::path::{Component, Path}; use crate::prelude::*; @@ -42,25 +41,19 @@ pub(crate) const MD5_LEN: usize = 16; pub struct FileHash([u8; MD5_LEN]); impl FileHash { - pub fn inner(self) -> [u8; MD5_LEN] { - self.0 - } -} - -pub struct UnsupportedHashType; - -impl TryFrom for FileHash { - type Error = UnsupportedHashType; - - fn try_from(hash: SourceFileHash) -> Result { + pub fn from_source_hash(hash: SourceFileHash) -> Option { if hash.kind == SourceFileHashAlgorithm::Md5 { let mut buf = [0u8; MD5_LEN]; buf.copy_from_slice(hash.hash_bytes()); - Ok(Self(buf)) + Some(Self(buf)) } else { - Err(UnsupportedHashType) + None } } + + pub fn inner(self) -> [u8; MD5_LEN] { + self.0 + } } fn line_program_add_file( @@ -86,9 +79,9 @@ fn line_program_add_file( line_strings, ); - let file_hash = FileHash::try_from(file.src_hash); + let file_hash = FileHash::from_source_hash(file.src_hash); - line_program.file_has_md5 = file_hash.is_ok(); + line_program.file_has_md5 = file_hash.is_some(); line_program.add_file(file_name, dir_id, Some(FileInfo { timestamp: 0, size: 0, diff --git a/src/debuginfo/mod.rs b/src/debuginfo/mod.rs index fbf5fa052362..042beb4de1bb 100644 --- a/src/debuginfo/mod.rs +++ b/src/debuginfo/mod.rs @@ -1,8 +1,6 @@ mod emit; mod line_info; -use std::convert::TryFrom; - use crate::prelude::*; use rustc_span::FileName; @@ -67,7 +65,8 @@ impl<'tcx> DebugContext<'tcx> { let hash = tcx.sess .source_map() .get_source_file(&FileName::Real(path)) - .and_then(|f| line_info::FileHash::try_from(f.src_hash).ok()); + .map(|f| f.src_hash) + .and_then(line_info::FileHash::from_source_hash); (name, hash) }, None => (tcx.crate_name(LOCAL_CRATE).to_string(), None),