readcon_core/lib.rs
1// A Rust library must not choose the process allocator for its consumers.
2// Final readcon artifacts opt into mimalloc with `mimalloc-allocator`: glibc's
3// allocator serializes the per-frame shells of a parallel parse on one arena
4// lock, while mimalloc lets those shells allocate on the parsing thread.
5// `asan_run` is set only for `-Zsanitizer=address`. mimalloc's `mi_free`
6// ignores the size Rust passes, so that run uses the system allocator.
7// `tsan_run` is set only for `-Zsanitizer=thread`. mimalloc is not built
8// with TSan, so TSan cannot see a block freed on one thread and reused on
9// another, and reports the reuse as a race. That run uses the system
10// allocator too.
11// Miri cannot execute mimalloc either, so it uses the system allocator too.
12// `static_system_alloc` remains an explicit override for the C static archive
13// and existing users, although system allocation is now the library default.
14#![allow(unexpected_cfgs)]
15#[cfg(all(
16 feature = "mimalloc-allocator",
17 not(any(asan_run, tsan_run, miri)),
18 not(feature = "static_system_alloc")
19))]
20#[global_allocator]
21static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
22
23#[cfg(all(
24 test,
25 feature = "mimalloc-allocator",
26 not(any(asan_run, tsan_run, miri)),
27 not(feature = "static_system_alloc")
28))]
29mod allocator_link_tests {
30 #[cfg(all(
31 not(any(asan_run, tsan_run, miri)),
32 not(feature = "static_system_alloc")
33 ))]
34 unsafe extern "C" {
35 fn mi_usable_size(ptr: *const std::ffi::c_void) -> usize;
36 }
37
38 /// Final artifacts that opt in use this allocator. `mi_usable_size` is
39 /// only meaningful for mimalloc. The static archive always opts out.
40 #[test]
41 fn the_shipped_allocator_is_mimalloc() {
42 let boxed = Box::new([0u8; 32]);
43 // SAFETY: `boxed` is live, non-null, and was allocated by the active
44 // mimalloc global allocator; `mi_usable_size` only reads its metadata.
45 let usable = unsafe { mi_usable_size(boxed.as_ptr() as *const std::ffi::c_void) };
46 assert!(
47 usable >= 32,
48 "global allocator is not mimalloc (usable size {usable})"
49 );
50 }
51}
52
53pub mod array;
54#[doc(hidden)]
55pub mod columns;
56pub mod compression;
57pub mod conbin;
58pub mod correspondence;
59#[cfg(feature = "cuda")]
60pub mod cuda_array;
61pub mod distrib;
62#[cfg(feature = "mpi")]
63pub mod distrib_read;
64#[cfg(feature = "mpi")]
65pub mod distrib_write;
66pub mod error;
67pub mod extxyz_scan;
68mod fast_dec;
69#[cfg(feature = "capi")]
70pub mod ffi;
71pub mod frame_index;
72#[cfg(feature = "grammar")]
73pub mod grammar;
74pub mod helpers;
75pub mod hostlabel;
76/// Campaign screening scalars / CON ingest contracts for corpus stores (`readcon-db`).
77pub mod index_proj;
78pub mod iterators;
79#[cfg(all(test, feature = "mpi"))]
80pub(crate) mod mpi_launch;
81pub mod parser;
82pub mod pdb_scan;
83/// RCSO/RCSB cooked numerics for a caller-side broadcast. No MPI in this crate.
84pub mod rcso;
85pub mod storage_dtype;
86pub mod stream;
87pub mod trajectory;
88pub mod types;
89pub mod units;
90pub mod writer;
91
92/// Foreign path / CON → CON write for stack migration (CLI + library).
93pub mod convert;
94
95#[cfg(feature = "metatensor")]
96pub mod metatensor_export;
97
98/// Chemfiles multi-format import (real impl behind `chemfiles` feature; stubs otherwise).
99pub mod chemfiles_import;
100
101/// Chemfiles selection grammar on CON frames (real impl behind `chemfiles` feature; stubs otherwise).
102pub mod chemfiles_selection;
103
104#[cfg(feature = "rpc")]
105pub mod rpc;
106
107// Re-export for generated capnp code which references crate::ReadCon_capnp
108#[cfg(feature = "rpc")]
109pub use rpc::read_con_capnp as ReadCon_capnp;
110
111#[cfg(feature = "python")]
112pub mod python;
113
114/// CON/convel format spec version implemented by this build.
115///
116/// - Version 1: column 5 present but semantics undefined. Readers MAY
117/// ignore it. No JSON metadata line.
118/// - Version 2: column 5 is the original atom index before type-based
119/// grouping; JSON line 2 with at least `{"con_spec_version": 2}`.
120/// - Version 3: same as v2 plus **required** `units` object on JSON **line 2**
121/// with non-empty `length` and `energy`. Every minor keeps the version 3.0
122/// meaning of `units.time`: velocities are `units.length` per the natural
123/// time unit, and `units.time` is not trusted (CON-units.8, CON-units.9).
124/// - Version 4: `units.time` is authoritative for velocities (CON-units.10).
125/// Writers emit canonical names (`angstrom`, `eV`) and, with velocities,
126/// `units.time`. The spec identity is major.minor.patch (CON-status.3).
127///
128/// See `docs/orgmode/spec.org` for the full specification.
129pub const CON_SPEC_VERSION: u32 = 4;
130
131/// Minor of major 4 implemented by this reader. A newer minor of major 4
132/// is accepted. An unknown section is skipped only when the frame minor is
133/// greater than the minor this reader implements for that major and
134/// `section_arity` gives the value-field count (CON-section.8).
135/// For major 3 that minor is 1.
136pub const CON_SPEC_MINOR: u32 = 0;
137
138/// Patch of this specification text (CON-status.3, CON-status.4).
139pub const CON_SPEC_PATCH: u32 = 4;
140
141/// Library version string, taken from the package version at compile time.
142/// `build.rs` checks that it matches the `VERSION` file.
143pub const VERSION: &str = env!("CARGO_PKG_VERSION");
144
145/// Breaking-change major for the public C ABI (also carried on RPC stamps).
146pub const RKR_ABI_VERSION_MAJOR: u32 = 2;
147/// Additive-change minor for the public C ABI (also carried on RPC stamps).
148pub const RKR_ABI_VERSION_MINOR: u32 = 0;
149/// Layout revision for opaque handles and exported records.
150pub const RKR_ABI_LAYOUT_REVISION: u32 = 0;
151
152pub use correspondence::{
153 CorrespondenceError, IdentityCheck, atom_id_permutation, check_atom_id_consistency,
154 minimum_image_delta, minimum_image_displacements, reorder_like, subframe,
155};
156
157#[cfg(test)]
158mod tests {
159 use super::*;
160
161 #[test]
162 fn test_spec_version_is_current() {
163 assert_eq!(CON_SPEC_VERSION, 4);
164 assert_eq!(CON_SPEC_MINOR, 0);
165 assert_eq!(CON_SPEC_PATCH, 4);
166 #[cfg(feature = "capi")]
167 assert_eq!(ffi::RKR_CON_SPEC_VERSION, CON_SPEC_VERSION);
168 }
169
170 #[test]
171 fn test_version_matches_source() {
172 let path = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("VERSION");
173 let text = std::fs::read_to_string(&path).unwrap();
174 assert_eq!(text, format!("{VERSION}\n"));
175 }
176}
177
178#[cfg(all(test, feature = "parallel", feature = "zstd"))]
179mod corpus31_manifest;