System: You complete Rust implementations to match C++ semantics precisely, keeping APIs stable. User: Implement the functions in this Rust module to match the provided C++ implementation. Requirements: - Keep signatures and types from the skeleton. - Preserve semantics; explicit RNG passed in where randomness is used. - Avoid panics; return `Result` on fallible paths. - Add/extend unit tests under `#[cfg(test)]` using given spec. Glossary: {{GLOSSARY}} Style: {{STYLE}} Determinism: {{DETERMINISM}} Current Rust module: ```rust {{SKELETON}} C++ impl (reference semantics): cpp Copy code {{SOURCE_CHUNK}} Test spec (pseudo or C++ tests): text Copy code {{TEST_SPEC}} Return the full updated Rust module.