#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![warn(missing_docs)]
#![warn(missing_debug_implementations)]
#![cfg_attr(not(feature = "simd"), forbid(unsafe_code))]
#![cfg_attr(feature = "simd", deny(unsafe_code))]
#![warn(clippy::all, clippy::pedantic)]
#![allow(clippy::missing_errors_doc)]
#![doc = include_str!("../README.md")]
#[cfg(feature = "unstable-bridge")]
pub mod bridge;
#[cfg(feature = "std")]
mod csr;
pub mod fixed;
#[cfg(feature = "unstable-bridge")]
pub mod kernel;
pub mod lif_neuron;
#[cfg(feature = "std")]
pub mod network;
pub mod nir;
#[cfg(feature = "simd")]
#[allow(unsafe_code)]
pub mod simd;
pub mod spike_recorder;
#[cfg(feature = "std")]
mod stats;
pub mod synapse;
pub mod trace;
pub mod trit;
pub use fixed::{FixedNetwork, FixedSynapse};
pub use lif_neuron::{LIFNeuron, NeuronType, VoltageResolution};
#[cfg(feature = "std")]
pub use network::{NetworkTopology, SpikingNeuralNetwork};
#[cfg(feature = "std")]
pub use nir::NirImport;
pub use nir::NirImportOptions;
#[cfg(feature = "unstable-stdp")]
pub use synapse::STDPRule;
pub use synapse::Synapse;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Error {
SelfConnection,
NeuronCountOutOfRange,
ZeroTimeStep,
FrozenMembrane,
NeuronIdOutOfRange,
DivisorOutOfRange,
BadLayerSizes,
MissingNeuronType,
ProbabilityOutOfRange,
PlasticityEnabled,
NeuronCountMismatch,
SynapseCountMismatch,
MixedVoltageGrids,
BadCaseName,
NeuronNotAtRest,
NeuronIdMismatch,
}
impl core::fmt::Display for Error {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str(match self {
Self::SelfConnection => "a synapse from a neuron to itself",
Self::NeuronCountOutOfRange => "a neuron count outside 1 to 65,535",
Self::ZeroTimeStep => "a time step of zero",
Self::FrozenMembrane => "a membrane time constant of 0 or above 1,000 time steps",
Self::NeuronIdOutOfRange => "a neuron id not below the neuron count",
Self::DivisorOutOfRange => "a synaptic input divisor outside 1 to 32,767",
Self::BadLayerSizes => {
"a layer of no neuron, or layer sizes that do not sum to the neuron count"
}
Self::MissingNeuronType => {
"a balanced network with no excitatory or no inhibitory neuron"
}
Self::ProbabilityOutOfRange => "a connectivity or rewiring probability outside 0 to 1",
Self::PlasticityEnabled => "a network with plasticity enabled",
Self::NeuronCountMismatch => "a neuron count other than the fixed network's N",
Self::SynapseCountMismatch => "a synapse count other than the fixed network's S",
Self::MixedVoltageGrids => "neurons on more than one voltage grid",
Self::BadCaseName => "a case name that is empty or outside [a-z0-9-]",
Self::NeuronNotAtRest => "a neuron that is not at rest",
Self::NeuronIdMismatch => "a neuron id other than its position",
})
}
}
impl core::error::Error for Error {}
const _: fn() = || {
fn a<T: core::error::Error>() {}
a::<Error>();
};
const _: () = assert!(core::mem::size_of::<Error>() == 1);
pub type Result<T> = core::result::Result<T, Error>;
#[cfg(all(test, feature = "std"))]
mod tests {
use super::Error;
use std::collections::BTreeSet;
#[test]
fn each_variant_prints_its_rule() {
let texts = [
(Error::SelfConnection, "a synapse from a neuron to itself"),
(
Error::NeuronCountOutOfRange,
"a neuron count outside 1 to 65,535",
),
(Error::ZeroTimeStep, "a time step of zero"),
(
Error::FrozenMembrane,
"a membrane time constant of 0 or above 1,000 time steps",
),
(
Error::NeuronIdOutOfRange,
"a neuron id not below the neuron count",
),
(
Error::DivisorOutOfRange,
"a synaptic input divisor outside 1 to 32,767",
),
(
Error::BadLayerSizes,
"a layer of no neuron, or layer sizes that do not sum to the neuron count",
),
(
Error::MissingNeuronType,
"a balanced network with no excitatory or no inhibitory neuron",
),
(
Error::ProbabilityOutOfRange,
"a connectivity or rewiring probability outside 0 to 1",
),
(
Error::PlasticityEnabled,
"a network with plasticity enabled",
),
(
Error::NeuronCountMismatch,
"a neuron count other than the fixed network's N",
),
(
Error::SynapseCountMismatch,
"a synapse count other than the fixed network's S",
),
(
Error::MixedVoltageGrids,
"neurons on more than one voltage grid",
),
(
Error::BadCaseName,
"a case name that is empty or outside [a-z0-9-]",
),
(Error::NeuronNotAtRest, "a neuron that is not at rest"),
(
Error::NeuronIdMismatch,
"a neuron id other than its position",
),
];
for (error, text) in texts {
match error {
Error::SelfConnection
| Error::NeuronCountOutOfRange
| Error::ZeroTimeStep
| Error::FrozenMembrane
| Error::NeuronIdOutOfRange
| Error::DivisorOutOfRange
| Error::BadLayerSizes
| Error::MissingNeuronType
| Error::ProbabilityOutOfRange
| Error::PlasticityEnabled
| Error::NeuronCountMismatch
| Error::SynapseCountMismatch
| Error::MixedVoltageGrids
| Error::BadCaseName
| Error::NeuronNotAtRest
| Error::NeuronIdMismatch => assert_eq!(error.to_string(), text),
}
assert!(text.starts_with(char::is_lowercase), "{text}");
assert!(!text.ends_with(['.', '!', '?']), "{text}");
}
let distinct: BTreeSet<&str> = texts.iter().map(|&(_, text)| text).collect();
assert_eq!(distinct.len(), texts.len(), "two variants print alike");
}
}