Skip to content

Commit

Permalink
Simplify FileHash ctor
Browse files Browse the repository at this point in the history
  • Loading branch information
l4l committed Apr 18, 2020
1 parent 710da05 commit 0e837e3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 19 deletions.
25 changes: 9 additions & 16 deletions 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::*;
Expand Down Expand Up @@ -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<SourceFileHash> for FileHash {
type Error = UnsupportedHashType;

fn try_from(hash: SourceFileHash) -> Result<Self, Self::Error> {
pub fn from_source_hash(hash: SourceFileHash) -> Option<Self> {
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(
Expand All @@ -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,
Expand Down
5 changes: 2 additions & 3 deletions src/debuginfo/mod.rs
@@ -1,8 +1,6 @@
mod emit;
mod line_info;

use std::convert::TryFrom;

use crate::prelude::*;

use rustc_span::FileName;
Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit 0e837e3

Please sign in to comment.