Closed
Description
Code
use std::fs;
use std::fs::File;
use std::io::Read;
use std::convert::TryInto;
fn get_file_as_byte_vec(filename: &String) -> Vec<u8> {
let mut f = File::open(&filename).expect("no file found");
let metadata = fs::metadata(&filename).expect("unable to read metadata");
let mut buffer = vec![0; metadata.len() as usize];
f.read(&mut buffer).expect("buffer overflow");
buffer
}
fn demo<T, const N: usize>(v: Vec<T>) -> [T; N] {
v.try_into()
.unwrap_or_else(|v: Vec<T>| panic!("Expected a Vec of length {} but it was {}", N, v.len()))
}
fn main() {
// Specify filepath
let file: &String = &String::from("SomeBinaryDataFileWith4ByteHeaders_f32s_and_u32s");
// Read file into a vector of bytes
let file_data = get_file_as_byte_vec(file);
// Print length of vector and first few values
let length = file_data.len();
println!("The read function read {} bytes", length);
println!("The first few bytes:");
for i in 0..20{
println!("{}", file_data[i]);
}
// Manually count just to make sure
let mut n: u64 = 0;
for data in file_data{
n += 1;
}
println!("We counted {} bytes", n);
assert!(n as usize == length, "Manual counting does not equal len method");
// Simulation parameters
const N: usize = 49627502; // Number of Particles
const bs: f64 = 125.0; // Box Size
const HEADER_INCREMENT: u64 = 4*1;
// Initialize index and counter variables
let (mut j, mut pos, mut vel, mut id, mut mass): (u64, u64, u64, u64, u64) = (0, 0, 0, 0, 0);
// Unpack Position Data
j += HEADER_INCREMENT;
let mut position: Vec<f32> = Vec::new();
while position.len() < N*3 {
let p: Vec<u8> = Vec::new();
for item in 0i8..4 {
let item = item;
p.push(file_data[j as usize]);
j += 1;
}
&mut position[position.len()] = f32::from_be_bytes(demo(p));
}
// Ensure position data is indeed position by checking values
for p in position {
assert!((p > 0.) & (p < 125.), "Not in box")
}
}
Meta
rustc --version --verbose
:
rustc 1.58.0 (02072b482 2022-01-11)
binary: rustc
commit-hash: 02072b482a8b5357f7fb5e5637444ae30e423c40
commit-date: 2022-01-11
host: x86_64-unknown-linux-gnu
release: 1.58.0
LLVM version: 13.0.0
Error output
warning: unused variable: `data`
--> fixbinary.rs:41:9
|
41 | for data in file_data{
| ^^^^ help: if this is intentional, prefix it with an underscore: `_data`
|
= note: `#[warn(unused_variables)]` on by default
warning: unused variable: `pos`
--> fixbinary.rs:53:21
|
53 | let (mut j, mut pos, mut vel, mut id, mut mass): (u64, u64, u64, u64, u64) = (0, 0, 0, 0, 0);
| ^^^ help: if this is intentional, prefix it with an underscore: `_pos`
warning: unused variable: `vel`
--> fixbinary.rs:53:30
|
53 | let (mut j, mut pos, mut vel, mut id, mut mass): (u64, u64, u64, u64, u64) = (0, 0, 0, 0, 0);
| ^^^ help: if this is intentional, prefix it with an underscore: `_vel`
warning: unused variable: `id`
--> fixbinary.rs:53:39
|
53 | let (mut j, mut pos, mut vel, mut id, mut mass): (u64, u64, u64, u64, u64) = (0, 0, 0, 0, 0);
| ^^ help: if this is intentional, prefix it with an underscore: `_id`
warning: unused variable: `mass`
--> fixbinary.rs:53:47
|
53 | let (mut j, mut pos, mut vel, mut id, mut mass): (u64, u64, u64, u64, u64) = (0, 0, 0, 0, 0);
| ^^^^ help: if this is intentional, prefix it with an underscore: `_mass`
warning: unused variable: `item`
--> fixbinary.rs:62:17
|
62 | let item = item;
| ^^^^ help: if this is intentional, prefix it with an underscore: `_item`
warning: constant is never used: `bs`
--> fixbinary.rs:49:5
|
49 | const bs: f64 = 125.0; // Box Size
| ^^^^^^^^^^^^^^^^^^^^^^
|
= note: `#[warn(dead_code)]` on by default
warning: constant `bs` should have an upper case name
--> fixbinary.rs:49:11
|
49 | const bs: f64 = 125.0; // Box Size
| ^^ help: convert the identifier to upper case: `BS`
|
= note: `#[warn(non_upper_case_globals)]` on by default
error: internal compiler error: compiler/rustc_codegen_llvm/src/context.rs:867:13: failed to get layout for `[type error]`: the type `[type error]` has an unknown layout
--> fixbinary.rs:23:1
|
23 | fn main() {
| ^^^^^^^^^
thread 'rustc' panicked at 'Box<dyn Any>', /rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/compiler/rustc_errors/src/lib.rs:1115:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
note: the compiler unexpectedly panicked. this is a bug.
note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md
note: rustc 1.58.0 (02072b482 2022-01-11) running on x86_64-unknown-linux-gnu
query stack during panic:
end of query stack
error: aborting due to previous error; 8 warnings emitted
Backtrace
thread 'rustc' panicked at 'Box<dyn Any>', /rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/compiler/rustc_errors/src/lib.rs:1115:9
stack backtrace:
0: 0x7f204083be5c - std::backtrace_rs::backtrace::libunwind::trace::h093d4af0eabdfc15
at /rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/std/src/../../backtrace/src/backtrace/libunwind.rs:93:5
1: 0x7f204083be5c - std::backtrace_rs::backtrace::trace_unsynchronized::h2b90813d74c759ca
at /rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
2: 0x7f204083be5c - std::sys_common::backtrace::_print_fmt::hfaa8856bf3eca13f
at /rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/std/src/sys_common/backtrace.rs:67:5
3: 0x7f204083be5c - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h0cbaef3adcb5a454
at /rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/std/src/sys_common/backtrace.rs:46:22
4: 0x7f204089918c - core::fmt::write::h35a8eb836b847360
at /rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/core/src/fmt/mod.rs:1149:17
5: 0x7f204082bf55 - std::io::Write::write_fmt::h45f2b8390f189782
at /rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/std/src/io/mod.rs:1697:15
6: 0x7f204083f2b0 - std::sys_common::backtrace::_print::h56f62073b0e62985
at /rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/std/src/sys_common/backtrace.rs:49:5
7: 0x7f204083f2b0 - std::sys_common::backtrace::print::h152fba05ec38941b
at /rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/std/src/sys_common/backtrace.rs:36:9
8: 0x7f204083f2b0 - std::panicking::default_hook::{{closure}}::ha3121a0b8482251f
at /rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/std/src/panicking.rs:211:50
9: 0x7f204083ee65 - std::panicking::default_hook::hde5d78c11ae3b8f6
at /rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/std/src/panicking.rs:228:9
10: 0x7f2040fe8d71 - rustc_driver[b9de4364bfcde3cd]::DEFAULT_HOOK::{closure#0}::{closure#0}
11: 0x7f204083fac9 - std::panicking::rust_panic_with_hook::he6f55c3e7ed1777c
at /rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/std/src/panicking.rs:610:17
12: 0x7f2041130bfb - std[40fd643a72e9c5b]::panicking::begin_panic::<rustc_errors[2765bbf9a6752b72]::ExplicitBug>::{closure#0}
13: 0x7f2041130556 - std[40fd643a72e9c5b]::sys_common::backtrace::__rust_end_short_backtrace::<std[40fd643a72e9c5b]::panicking::begin_panic<rustc_errors[2765bbf9a6752b72]::ExplicitBug>::{closure#0}, !>
14: 0x7f204116718f - std[40fd643a72e9c5b]::panicking::begin_panic::<rustc_errors[2765bbf9a6752b72]::ExplicitBug>
15: 0x7f204115e52d - std[40fd643a72e9c5b]::panic::panic_any::<rustc_errors[2765bbf9a6752b72]::ExplicitBug>
16: 0x7f204115d338 - <rustc_errors[2765bbf9a6752b72]::HandlerInner>::span_bug::<rustc_span[87dd3c90247a33c7]::span_encoding::Span>
17: 0x7f204115d230 - <rustc_errors[2765bbf9a6752b72]::Handler>::span_bug::<rustc_span[87dd3c90247a33c7]::span_encoding::Span>
18: 0x7f2041154552 - rustc_middle[da375b81956c4f3d]::ty::context::tls::with_opt::<rustc_middle[da375b81956c4f3d]::util::bug::opt_span_bug_fmt<rustc_span[87dd3c90247a33c7]::span_encoding::Span>::{closure#0}, ()>
19: 0x7f20411543e0 - rustc_middle[da375b81956c4f3d]::util::bug::opt_span_bug_fmt::<rustc_span[87dd3c90247a33c7]::span_encoding::Span>
20: 0x7f20411543ac - rustc_middle[da375b81956c4f3d]::util::bug::span_bug_fmt::<rustc_span[87dd3c90247a33c7]::span_encoding::Span>
21: 0x7f204112b7b6 - <rustc_codegen_llvm[7e58ca7f3e3a6114]::context::CodegenCx as rustc_middle[da375b81956c4f3d]::ty::layout::LayoutOfHelpers>::handle_layout_err
22: 0x7f20411291fa - <rustc_codegen_llvm[7e58ca7f3e3a6114]::context::CodegenCx as rustc_middle[da375b81956c4f3d]::ty::layout::LayoutOf>::spanned_layout_of::{closure#0}
23: 0x7f2042247f2e - <rustc_codegen_llvm[7e58ca7f3e3a6114]::context::CodegenCx as rustc_middle[da375b81956c4f3d]::ty::layout::LayoutOf>::spanned_layout_of
24: 0x7f2042246f8e - <core[5959bd7a20bc512c]::iter::adapters::map::Map<core[5959bd7a20bc512c]::slice::iter::Iter<rustc_middle[da375b81956c4f3d]::mir::LocalDecl>, rustc_codegen_ssa[131e031ff59460c4]::mir::analyze::non_ssa_locals<rustc_codegen_llvm[7e58ca7f3e3a6114]::builder::Builder>::{closure#0}> as core[5959bd7a20bc512c]::iter::traits::iterator::Iterator>::fold::<(), core[5959bd7a20bc512c]::iter::traits::iterator::Iterator::for_each::call<rustc_codegen_ssa[131e031ff59460c4]::mir::analyze::LocalKind, <alloc[57a5c12a58f2352a]::vec::Vec<rustc_codegen_ssa[131e031ff59460c4]::mir::analyze::LocalKind> as alloc[57a5c12a58f2352a]::vec::spec_extend::SpecExtend<rustc_codegen_ssa[131e031ff59460c4]::mir::analyze::LocalKind, core[5959bd7a20bc512c]::iter::adapters::map::Map<core[5959bd7a20bc512c]::slice::iter::Iter<rustc_middle[da375b81956c4f3d]::mir::LocalDecl>, rustc_codegen_ssa[131e031ff59460c4]::mir::analyze::non_ssa_locals<rustc_codegen_llvm[7e58ca7f3e3a6114]::builder::Builder>::{closure#0}>>>::spec_extend::{closure#0}>::{closure#0}>
25: 0x7f2042274483 - rustc_codegen_ssa[131e031ff59460c4]::mir::analyze::non_ssa_locals::<rustc_codegen_llvm[7e58ca7f3e3a6114]::builder::Builder>
26: 0x7f204226063d - rustc_codegen_ssa[131e031ff59460c4]::mir::codegen_mir::<rustc_codegen_llvm[7e58ca7f3e3a6114]::builder::Builder>
27: 0x7f2042244cc4 - rustc_codegen_ssa[131e031ff59460c4]::base::codegen_instance::<rustc_codegen_llvm[7e58ca7f3e3a6114]::builder::Builder>
28: 0x7f2042254efe - <rustc_middle[da375b81956c4f3d]::mir::mono::MonoItem as rustc_codegen_ssa[131e031ff59460c4]::mono_item::MonoItemExt>::define::<rustc_codegen_llvm[7e58ca7f3e3a6114]::builder::Builder>
29: 0x7f2042258e50 - rustc_codegen_llvm[7e58ca7f3e3a6114]::base::compile_codegen_unit::module_codegen
30: 0x7f2042bfb176 - <rustc_query_system[54375e604b07d15e]::dep_graph::graph::DepGraph<rustc_middle[da375b81956c4f3d]::dep_graph::dep_node::DepKind>>::with_task::<rustc_middle[da375b81956c4f3d]::ty::context::TyCtxt, rustc_span[87dd3c90247a33c7]::symbol::Symbol, rustc_codegen_ssa[131e031ff59460c4]::ModuleCodegen<rustc_codegen_llvm[7e58ca7f3e3a6114]::ModuleLlvm>>
31: 0x7f2042c57337 - rustc_codegen_llvm[7e58ca7f3e3a6114]::base::compile_codegen_unit
32: 0x7f2042c11499 - rustc_codegen_ssa[131e031ff59460c4]::base::codegen_crate::<rustc_codegen_llvm[7e58ca7f3e3a6114]::LlvmCodegenBackend>
33: 0x7f2042c3bb7a - <rustc_codegen_llvm[7e58ca7f3e3a6114]::LlvmCodegenBackend as rustc_codegen_ssa[131e031ff59460c4]::traits::backend::CodegenBackend>::codegen_crate
34: 0x7f2042bc761e - <rustc_interface[3fb879ef72273690]::queries::Queries>::ongoing_codegen
35: 0x7f2042b8baba - <rustc_interface[3fb879ef72273690]::interface::Compiler>::enter::<rustc_driver[b9de4364bfcde3cd]::run_compiler::{closure#1}::{closure#2}, core[5959bd7a20bc512c]::result::Result<core[5959bd7a20bc512c]::option::Option<rustc_interface[3fb879ef72273690]::queries::Linker>, rustc_errors[2765bbf9a6752b72]::ErrorReported>>
36: 0x7f2042b87984 - rustc_span[87dd3c90247a33c7]::with_source_map::<core[5959bd7a20bc512c]::result::Result<(), rustc_errors[2765bbf9a6752b72]::ErrorReported>, rustc_interface[3fb879ef72273690]::interface::create_compiler_and_run<core[5959bd7a20bc512c]::result::Result<(), rustc_errors[2765bbf9a6752b72]::ErrorReported>, rustc_driver[b9de4364bfcde3cd]::run_compiler::{closure#1}>::{closure#1}>
37: 0x7f2042b8c8df - <scoped_tls[2c65bc5c0a345aa9]::ScopedKey<rustc_span[87dd3c90247a33c7]::SessionGlobals>>::set::<rustc_interface[3fb879ef72273690]::util::setup_callbacks_and_run_in_thread_pool_with_globals<rustc_interface[3fb879ef72273690]::interface::run_compiler<core[5959bd7a20bc512c]::result::Result<(), rustc_errors[2765bbf9a6752b72]::ErrorReported>, rustc_driver[b9de4364bfcde3cd]::run_compiler::{closure#1}>::{closure#0}, core[5959bd7a20bc512c]::result::Result<(), rustc_errors[2765bbf9a6752b72]::ErrorReported>>::{closure#0}::{closure#0}, core[5959bd7a20bc512c]::result::Result<(), rustc_errors[2765bbf9a6752b72]::ErrorReported>>
38: 0x7f2042b8aa15 - std[40fd643a72e9c5b]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[3fb879ef72273690]::util::setup_callbacks_and_run_in_thread_pool_with_globals<rustc_interface[3fb879ef72273690]::interface::run_compiler<core[5959bd7a20bc512c]::result::Result<(), rustc_errors[2765bbf9a6752b72]::ErrorReported>, rustc_driver[b9de4364bfcde3cd]::run_compiler::{closure#1}>::{closure#0}, core[5959bd7a20bc512c]::result::Result<(), rustc_errors[2765bbf9a6752b72]::ErrorReported>>::{closure#0}, core[5959bd7a20bc512c]::result::Result<(), rustc_errors[2765bbf9a6752b72]::ErrorReported>>
39: 0x7f2042bb4baa - <<std[40fd643a72e9c5b]::thread::Builder>::spawn_unchecked<rustc_interface[3fb879ef72273690]::util::setup_callbacks_and_run_in_thread_pool_with_globals<rustc_interface[3fb879ef72273690]::interface::run_compiler<core[5959bd7a20bc512c]::result::Result<(), rustc_errors[2765bbf9a6752b72]::ErrorReported>, rustc_driver[b9de4364bfcde3cd]::run_compiler::{closure#1}>::{closure#0}, core[5959bd7a20bc512c]::result::Result<(), rustc_errors[2765bbf9a6752b72]::ErrorReported>>::{closure#0}, core[5959bd7a20bc512c]::result::Result<(), rustc_errors[2765bbf9a6752b72]::ErrorReported>>::{closure#1} as core[5959bd7a20bc512c]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
40: 0x7f204084abf3 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h3604301cdaaa9dbf
at /rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/alloc/src/boxed.rs:1694:9
41: 0x7f204084abf3 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h4cf736d2de892eff
at /rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/alloc/src/boxed.rs:1694:9
42: 0x7f204084abf3 - std::sys::unix::thread::Thread::new::thread_start::h71a82d4ee5b02c9b
at /rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/std/src/sys/unix/thread.rs:106:17
43: 0x7f204058bea5 - start_thread
44: 0x7f203fea89fd - __clone
45: 0x0 - <unknown>
I'm trying to read in a binary file (in a probably odd way). I'm just barely learning Rust; started <1 week ago. Sorry for breaking things.