Skip to main content

ncnn_rust/
lib.rs

1//! The crate documentation is the package README, so the page crates.io renders and the page
2//! rustdoc builds cannot drift apart: one file answers both.
3//!
4#![doc = include_str!("../README.md")]
5#![deny(missing_docs)]
6
7pub mod alloc;
8pub mod cpu;
9#[cfg(feature = "vulkan")]
10pub mod gpu;
11pub mod heap;
12pub mod image;
13/// Raw ncnn bindings and the project's C adapters.
14///
15/// These items follow ncnn's C ABI rather than the safe crate's contracts, and they carry no
16/// independent semver guarantee: they move with this crate's version. The invariants in the safety
17/// contracts apply when they are called directly.
18///
19/// The module is behind the `raw` feature, so the default build publishes no generated bindings.
20#[cfg(feature = "raw")]
21pub use ncnn_sys as sys;
22
23// The crate's own code reaches the generated bindings under the same name either way: a `pub`
24// re-export is visible inside the crate too, so the two arms never overlap.
25#[cfg(not(feature = "raw"))]
26pub(crate) use ncnn_sys as sys;
27
28mod error;
29mod layer;
30mod mat;
31mod net;
32mod options;
33mod reader;
34pub use reader::DataReader;
35mod param;
36
37pub use error::{Error, Result};
38pub use layer::{layer_type_index, GraphLayer, Layer, ModelBin};
39pub use mat::{
40    BorderMode, Borders, ElementKind, Mat, MatView, PixelFormat, ReshapeView, TensorInfo,
41};
42pub use net::{
43    BlobId, BlobInfo, Endpoint, Extractor, FinalOutput, Input, LayerInfo, Net, NetBuilder,
44    OutputGuard, OutputRange, ParamSource, ShapeInfo, TensorWidth, WeightSource,
45};
46pub use options::{CpuOption, OptionFlag, Options};
47pub use param::{ParamDict, ParamKind};
48
49/// Returns the pinned native version.
50pub fn version() -> &'static str {
51    unsafe {
52        std::ffi::CStr::from_ptr(sys::ncnn_version())
53            .to_str()
54            .unwrap_or("unknown")
55    }
56}
57
58/// Returns the pinned native version as the integer ncnn encodes, which is the release date.
59///
60/// Compare against a known value to decide whether a newer declaration is available; the string
61/// form is in [`version`].
62pub fn version_number() -> i32 {
63    unsafe { sys::ncnn_version_number() }
64}
65
66/// Returns the native logical CPU count.
67pub fn cpu_count() -> usize {
68    unsafe { sys::ncnn_rs_cpu_count().max(1) as usize }
69}