summaryrefslogtreecommitdiffstats
path: root/third_party/rust/glean-core/src/traits
diff options
context:
space:
mode:
Diffstat (limited to 'third_party/rust/glean-core/src/traits')
-rw-r--r--third_party/rust/glean-core/src/traits/custom_distribution.rs16
-rw-r--r--third_party/rust/glean-core/src/traits/mod.rs6
-rw-r--r--third_party/rust/glean-core/src/traits/object.rs53
-rw-r--r--third_party/rust/glean-core/src/traits/timing_distribution.rs25
4 files changed, 100 insertions, 0 deletions
diff --git a/third_party/rust/glean-core/src/traits/custom_distribution.rs b/third_party/rust/glean-core/src/traits/custom_distribution.rs
index c0c80c028b..43dfdb7da8 100644
--- a/third_party/rust/glean-core/src/traits/custom_distribution.rs
+++ b/third_party/rust/glean-core/src/traits/custom_distribution.rs
@@ -28,6 +28,22 @@ pub trait CustomDistribution {
/// them.
fn accumulate_samples_signed(&self, samples: Vec<i64>);
+ /// Accumulates precisely one signed sample in the metric.
+ ///
+ /// This is required so that the platform-specific code can provide us with a
+ /// 64 bit signed integer if no `u64` comparable type is available. This
+ /// will take care of filtering and reporting errors.
+ ///
+ /// # Arguments
+ ///
+ /// - `sample` - The singular sample to be recorded by the metric.
+ ///
+ /// ## Notes
+ ///
+ /// Discards any negative value of `sample` and reports an
+ /// [`ErrorType::InvalidValue`](crate::ErrorType::InvalidValue).
+ fn accumulate_single_sample_signed(&self, sample: i64);
+
/// **Exported for test purposes.**
///
/// Gets the currently stored histogram.
diff --git a/third_party/rust/glean-core/src/traits/mod.rs b/third_party/rust/glean-core/src/traits/mod.rs
index c4bcf7cdd6..4115609fdd 100644
--- a/third_party/rust/glean-core/src/traits/mod.rs
+++ b/third_party/rust/glean-core/src/traits/mod.rs
@@ -7,6 +7,10 @@
//! Individual metric types implement this trait to expose the specific metrics API.
//! It can be used by wrapping implementations to guarantee API conformance.
+/// Re-export for use in generated code.
+#[doc(hidden)]
+pub extern crate serde as __serde;
+
mod boolean;
mod counter;
mod custom_distribution;
@@ -15,6 +19,7 @@ mod event;
mod labeled;
mod memory_distribution;
mod numerator;
+mod object;
mod ping;
mod quantity;
mod rate;
@@ -37,6 +42,7 @@ pub use self::event::NoExtraKeys;
pub use self::labeled::Labeled;
pub use self::memory_distribution::MemoryDistribution;
pub use self::numerator::Numerator;
+pub use self::object::{ObjectError, ObjectSerialize};
pub use self::ping::Ping;
pub use self::quantity::Quantity;
pub use self::rate::Rate;
diff --git a/third_party/rust/glean-core/src/traits/object.rs b/third_party/rust/glean-core/src/traits/object.rs
new file mode 100644
index 0000000000..c579efeac7
--- /dev/null
+++ b/third_party/rust/glean-core/src/traits/object.rs
@@ -0,0 +1,53 @@
+// This Source Code Form is subject to the terms of the Mozilla Public
+// License, v. 2.0. If a copy of the MPL was not distributed with this
+// file, You can obtain one at https://mozilla.org/MPL/2.0/.
+
+use std::fmt::Display;
+
+use serde::{Deserialize, Serialize};
+use serde_json::Value as JsonValue;
+
+/// This type represents all possible errors that can occur when serializing or deserializing an object from/to JSON.
+#[derive(Debug)]
+pub struct ObjectError(serde_json::Error);
+
+impl Display for ObjectError {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ Display::fmt(&self.0, f)
+ }
+}
+
+impl std::error::Error for ObjectError {}
+
+/// An object that can be serialized into JSON.
+///
+/// Objects are defined by their structure in the metrics definition.
+///
+/// This is essentially a wrapper around serde's `Serialize`/`Deserialize`,
+/// but in a way we can name it for our JSON (de)serialization.
+pub trait ObjectSerialize {
+ /// Deserialize the object from its JSON representation.
+ ///
+ /// Returns an error if deserialization fails.
+ /// This should not happen for glean_parser-generated and later serialized objects.
+ fn from_str(obj: &str) -> Result<Self, ObjectError>
+ where
+ Self: Sized;
+
+ /// Serialize this object into a JSON string.
+ fn into_serialized_object(self) -> Result<JsonValue, ObjectError>;
+}
+
+impl<V> ObjectSerialize for V
+where
+ V: Serialize,
+ V: for<'de> Deserialize<'de>,
+{
+ fn from_str(obj: &str) -> Result<Self, ObjectError> {
+ serde_json::from_str(obj).map_err(ObjectError)
+ }
+
+ fn into_serialized_object(self) -> Result<JsonValue, ObjectError> {
+ serde_json::to_value(self).map_err(ObjectError)
+ }
+}
diff --git a/third_party/rust/glean-core/src/traits/timing_distribution.rs b/third_party/rust/glean-core/src/traits/timing_distribution.rs
index 03083753c6..ba618d2b4b 100644
--- a/third_party/rust/glean-core/src/traits/timing_distribution.rs
+++ b/third_party/rust/glean-core/src/traits/timing_distribution.rs
@@ -66,6 +66,31 @@ pub trait TimingDistribution {
/// are longer than `MAX_SAMPLE_TIME`.
fn accumulate_samples(&self, samples: Vec<i64>);
+ /// Accumulates precisely one signed sample in the metric.
+ ///
+ /// Precludes the need for a collection in the most common use case.
+ ///
+ /// Sign is required so that the platform-specific code can provide us with
+ /// a 64 bit signed integer if no `u64` comparable type is available. This
+ /// will take care of filtering and reporting errors for any provided negative
+ /// sample.
+ ///
+ /// Please note that this assumes that the provided sample is already in
+ /// the "unit" declared by the instance of the metric type (e.g. if the
+ /// instance this method was called on is using [`crate::TimeUnit::Second`], then
+ /// `sample` is assumed to be in that unit).
+ ///
+ /// # Arguments
+ ///
+ /// * `sample` - The singular sample to be recorded by the metric.
+ ///
+ /// ## Notes
+ ///
+ /// Discards any negative value and reports an [`ErrorType::InvalidValue`].
+ /// Reports an [`ErrorType::InvalidOverflow`] error if the sample is longer than
+ /// `MAX_SAMPLE_TIME`.
+ fn accumulate_single_sample(&self, sample: i64);
+
/// Accumulates the provided samples in the metric.
///
/// # Arguments