[go: up one dir, main page]

odds/
lib.rs

1//! Odds and ends — collection miscellania.
2//!
3//! - Utilities for debug-checked, release-unchecked indexing and slicing
4//! - Fixpoint combinator for closures
5//! - String and Vec extensions
6//!
7//! The **odds** crate has the following crate feature flags:
8//!
9//! - `std-vec`
10//!   - Enable Vec extensions
11//!   - Implies `std`
12//! - `std-string`
13//!   - Enable String extensions
14//!   - Implies `std`
15//! - `std`
16//!   - Enable basic libstd usage.
17//! - `unstable`.
18//!   - Optional.
19//!   - Requires nightly channel.
20//!   - Implement the closure traits for **Fix**.
21//!
22//! If none of the std features are enabled (they are not enabled by default),
23//! then the crate is `no_std`.
24//!
25//! # Rust Version
26//!
27//! This version of the crate requires Rust 1.15 or later.
28//!
29
30#![doc(html_root_url = "https://docs.rs/odds/0.4/")]
31#![cfg_attr(feature="unstable", feature(unboxed_closures, fn_traits))]
32
33#![cfg_attr(not(feature = "std"), no_std)]
34
35#[cfg(not(feature = "std"))]
36extern crate core as std;
37extern crate rawslice;
38extern crate rawpointer;
39extern crate unchecked_index;
40
41mod range;
42#[path = "fix.rs"]
43mod fix_impl;
44pub mod char;
45pub mod string;
46pub mod vec;
47pub mod slice;
48pub mod stride;
49
50pub use crate::fix_impl::Fix;
51pub use crate::fix_impl::fix;
52pub use crate::range::IndexRange;
53
54use crate::std::mem;
55
56/// prelude of often used traits and functions
57pub mod prelude {
58    pub use crate::slice::SliceFind;
59    pub use crate::string::StrExt;
60    pub use crate::string::StrChunksWindows;
61    #[cfg(feature = "std-string")]
62    pub use string::StringExt;
63    #[cfg(feature = "std-vec")]
64    pub use vec::{vec, VecExt};
65    #[doc(no_inline)]
66    pub use crate::IndexRange;
67    #[doc(no_inline)]
68    pub use crate::fix;
69}
70
71/// Safe to use with any wholly initialized memory `ptr`
72#[inline]
73pub unsafe fn raw_byte_repr<T: ?Sized>(ptr: &T) -> &[u8] {
74    std::slice::from_raw_parts(
75        ptr as *const _ as *const u8,
76        mem::size_of_val(ptr),
77    )
78}
79
80#[inline(always)]
81unsafe fn unreachable() -> ! {
82    enum Void { }
83    match *(1 as *const Void) {
84        /* unreachable */
85    }
86}
87
88/// Act as `debug_assert!` in debug mode, asserting that this point is not reached.
89///
90/// In release mode, no checks are done, and it acts like the `unreachable` intrinsic.
91#[inline(always)]
92pub unsafe fn debug_assert_unreachable() -> ! {
93    debug_assert!(false, "Entered unreachable section, this is a bug!");
94    unreachable()
95}
96
97/// Create a length 1 slice out of a reference
98pub fn ref_slice<T>(ptr: &T) -> &[T] {
99    unsafe {
100        std::slice::from_raw_parts(ptr, 1)
101    }
102}
103
104/// Create a length 1 mutable slice out of a reference
105pub fn ref_slice_mut<T>(ptr: &mut T) -> &mut [T] {
106    unsafe {
107        std::slice::from_raw_parts_mut(ptr, 1)
108    }
109}
110
111
112#[test]
113fn test_repr() {
114    unsafe {
115        assert_eq!(raw_byte_repr(&17u8), &[17]);
116        assert_eq!(raw_byte_repr("abc"), "abc".as_bytes());
117    }
118}