summaryrefslogtreecommitdiff
path: root/candle-nn/src/func.rs
blob: c9ec287ae469885da95734ea7042e3c6fdfd7a3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//! Layers defined by closures.
use candle::{Result, Tensor};

/// A layer defined by a simple closure.
pub struct Func<'a> {
    #[allow(clippy::type_complexity)]
    f: Box<dyn 'a + Fn(&Tensor) -> Result<Tensor> + Send>,
}

impl<'a> std::fmt::Debug for Func<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "func")
    }
}

pub fn func<'a, F>(f: F) -> Func<'a>
where
    F: 'a + Fn(&Tensor) -> Result<Tensor> + Send,
{
    Func { f: Box::new(f) }
}

impl<'a> super::Module for Func<'a> {
    fn forward(&self, xs: &Tensor) -> Result<Tensor> {
        (*self.f)(xs)
    }
}