quantsupport 0.1.0

Rust library for fixed-income, derivative pricing and risk analytics.
Documentation
use std::cell::{Ref, RefMut};

use crate::{
    ad::adreal::ADReal,
    core::{marketdatahandling::constructedelementstore::SharedElement, pillars::Pillars},
    indices::marketindex::MarketIndex,
    volatility::volatilitysurface::VolatilitySurface,
};

/// [`ADVolatilitySurfaceElement`] is a trait to identify AD-enable volatility surfaces.
pub trait ADVolatilitySurfaceElement:
    VolatilitySurface<ADReal> + Pillars<ADReal> + Send + Sync
{
}

/// [`VolatilitySurfaceElement`] is a handle to a particular [`ADVolatilitySurfaceElement`].
#[derive(Clone)]
pub struct VolatilitySurfaceElement {
    market_index: MarketIndex,
    surface: SharedElement<dyn ADVolatilitySurfaceElement>,
}

impl VolatilitySurfaceElement {
    /// Creates a new [`VolatilitySurfaceElement`] with the specified market index and surface.
    #[must_use]
    pub fn new(
        market_index: MarketIndex,
        surface: SharedElement<dyn ADVolatilitySurfaceElement>,
    ) -> Self {
        Self {
            market_index,
            surface,
        }
    }
    /// Returns the market index associated with the volatility surface element.
    #[must_use]
    pub const fn market_index(&self) -> &MarketIndex {
        &self.market_index
    }

    /// Returns a reference to the surface associated with the volatility surface element.
    #[must_use]
    pub fn surface(&self) -> Ref<'_, dyn ADVolatilitySurfaceElement> {
        self.surface.borrow()
    }

    /// Returns a mutable reference to the surface associated with the volatility surface element.
    #[must_use]
    pub fn surface_mut(&mut self) -> RefMut<'_, dyn ADVolatilitySurfaceElement> {
        self.surface.borrow_mut()
    }
}