From 14d566ee54aa049719b84b8f6480571a4ebe8f6b Mon Sep 17 00:00:00 2001 From: Sean Lynch Date: Mon, 20 Jan 2025 18:42:35 -0800 Subject: [PATCH] Allow constructing SampleId from Samples This is needed in order to support generic logic to read a SampleId from all records except MMAP. --- CHANGELOG.md | 2 ++ src/records/mod.rs | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d02f102..c78b72d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Added +- Allow constructing a `SampleId` from a `Sample`. ## [0.1.7] - 2025-01-18 ### Fixed diff --git a/src/records/mod.rs b/src/records/mod.rs index e126bad..19ec154 100644 --- a/src/records/mod.rs +++ b/src/records/mod.rs @@ -80,6 +80,19 @@ use crate::prelude::*; pub struct SampleId(sample_id::SampleId); impl SampleId { + /// Construct a `SampleId` by reading its fields out of a full sample + /// struct. + pub fn from_sample(sample: &Sample<'_>) -> Self { + Self(sample_id::SampleId::new( + sample.pid(), + sample.tid(), + sample.time(), + sample.id(), + sample.stream_id(), + sample.cpu(), + )) + } + /// The process ID that generated this event. pub fn pid(&self) -> Option { self.0.pid().copied() @@ -173,6 +186,18 @@ impl fmt::Debug for SampleId { } } +impl From<&'_ Sample<'_>> for SampleId { + fn from(value: &Sample) -> Self { + Self::from_sample(value) + } +} + +impl From> for SampleId { + fn from(value: Sample<'_>) -> Self { + Self::from_sample(&value) + } +} + /// A record emitted by the linux kernel. /// /// This enum contains every supported record type emitted by the kernel.