metrique-core 0.1.22

Library for wide event metrics - core traits
Documentation
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

// Delegate Entry impls for references and standard containers

use std::{borrow::Cow, sync::Arc};

use metrique_writer_core::{EntryWriter, entry::SampleGroupElement};

use crate::{InflectableEntry, namestyle::NameStyle};

impl<NS: NameStyle, T: InflectableEntry<NS>> InflectableEntry<NS> for &T {
    fn write<'a>(&'a self, writer: &mut impl EntryWriter<'a>) {
        (**self).write(writer)
    }

    fn sample_group(&self) -> impl Iterator<Item = SampleGroupElement> {
        (**self).sample_group()
    }

    fn descriptors(&self) -> metrique_writer_core::Descriptors<'_> {
        (**self).descriptors()
    }
}

impl<NS: NameStyle, T: InflectableEntry<NS>> InflectableEntry<NS> for Option<T> {
    fn write<'a>(&'a self, writer: &mut impl EntryWriter<'a>) {
        if let Some(entry) = self.as_ref() {
            entry.write(writer)
        }
    }

    fn sample_group(&self) -> impl Iterator<Item = SampleGroupElement> {
        self.as_ref().into_iter().flat_map(|e| e.sample_group())
    }

    fn descriptors(&self) -> metrique_writer_core::Descriptors<'_> {
        match self.as_ref() {
            Some(e) => e.descriptors(),
            None => metrique_writer_core::Descriptors::available(std::iter::empty()),
        }
    }
}

impl<NS: NameStyle, T: InflectableEntry<NS> + ?Sized> InflectableEntry<NS> for Box<T> {
    fn write<'a>(&'a self, writer: &mut impl EntryWriter<'a>) {
        (**self).write(writer)
    }

    fn sample_group(&self) -> impl Iterator<Item = SampleGroupElement> {
        (**self).sample_group()
    }

    fn descriptors(&self) -> metrique_writer_core::Descriptors<'_> {
        (**self).descriptors()
    }
}

impl<NS: NameStyle, T: InflectableEntry<NS> + ?Sized> InflectableEntry<NS> for Arc<T> {
    fn write<'a>(&'a self, writer: &mut impl EntryWriter<'a>) {
        (**self).write(writer)
    }

    fn sample_group(&self) -> impl Iterator<Item = SampleGroupElement> {
        (**self).sample_group()
    }

    fn descriptors(&self) -> metrique_writer_core::Descriptors<'_> {
        (**self).descriptors()
    }
}

impl<NS: NameStyle, T: InflectableEntry<NS> + ToOwned + ?Sized> InflectableEntry<NS>
    for Cow<'_, T>
{
    fn write<'a>(&'a self, writer: &mut impl EntryWriter<'a>) {
        (**self).write(writer)
    }

    fn sample_group(&self) -> impl Iterator<Item = SampleGroupElement> {
        (**self).sample_group()
    }

    fn descriptors(&self) -> metrique_writer_core::Descriptors<'_> {
        (**self).descriptors()
    }
}