Received: by 2002:a05:6358:7058:b0:131:369:b2a3 with SMTP id 24csp2255850rwp; Fri, 14 Jul 2023 03:26:48 -0700 (PDT) X-Google-Smtp-Source: APBJJlGa8okXf1dBibsl9hTiLimapGy/hq8tHiLk+mMg7Yq1wx6CByW6dYKCX3b4JPMz0ufQKKPU X-Received: by 2002:a05:6a20:542:b0:12d:1d8f:6af8 with SMTP id 2-20020a056a20054200b0012d1d8f6af8mr2728146pzq.18.1689330408266; Fri, 14 Jul 2023 03:26:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689330408; cv=none; d=google.com; s=arc-20160816; b=v+CLK9kx+Xwdayz/61JBLcUu/Sm6NKyqe9YeFsLgKD9lZ/Z46XD+cdMM7+jOC4eTRx Xy+KUyamS6dTTed3/E88EsuwxLHioZV5cDKiJ47RwG2KmHeJpYudNRgOFpeLm3kUuND4 fgwYO13BKyvRLELPm+qrlzzdK1uVPD4SamOPj+cffXfg6wtCGwGKHYnDjsfPxVVnfi6p DiBN56UeTgWjR1kD1D1Pub6AWIAywng/R30texWN6WuE1DI6H8A8g18Gd4UuWkxvZYL5 tKT6O5quEDCNH/9jCC4ieYvvHo22KfzyZsXUVZ/B1E7FfNZbAJijLZ6ropnbYU1TA8n2 S3yg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=Q5MF44XQzouX2FbGdHpxAthvfg7u3iO2oNPh/cM8NKE=; fh=Mf/id1NxShKXxE3h/15a4iS6BcfEagRNnHh6f/+D0VQ=; b=FpRGe0YFwSimcV8efExITNHN6g6fIfpRhaR6wPi0l9yaRyRTQn8HIcaGwyuecHQ3wq xfcPKFM/QVob1ZkYKULuXdy/8VVYO2IgBlUc3dZeiJiLNvUo4DtAMm+K2znjXlQkkul8 VsbaXWtsGviEQU+JRSvAGG/r7upkZjuYRrs9zRn+JBDkQXcQyo5ZjrIQ/sJIHlT3cN8r 7bVAyoVBDbJ3BeMO89GsRaKLVgO4n8XSxo0s0VloaGBLaUsZ37YU0KMQ7dggr8fegzyz twQMK80pJLJY2LWE/dZcWcBcikX6kWYIKPFzXI2aFXaRwDWEi+uvzDbfmETxMOvGyZJn CT5Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=apJ81VA8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y185-20020a6264c2000000b00681ad13f7cbsi6628538pfb.121.2023.07.14.03.26.36; Fri, 14 Jul 2023 03:26:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=apJ81VA8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235651AbjGNKFx (ORCPT + 99 others); Fri, 14 Jul 2023 06:05:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52230 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235800AbjGNKFw (ORCPT ); Fri, 14 Jul 2023 06:05:52 -0400 Received: from mail-ed1-x549.google.com (mail-ed1-x549.google.com [IPv6:2a00:1450:4864:20::549]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E552E3A86 for ; Fri, 14 Jul 2023 03:05:28 -0700 (PDT) Received: by mail-ed1-x549.google.com with SMTP id 4fb4d7f45d1cf-51dd4df2477so1038412a12.2 for ; Fri, 14 Jul 2023 03:05:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1689329127; x=1691921127; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Q5MF44XQzouX2FbGdHpxAthvfg7u3iO2oNPh/cM8NKE=; b=apJ81VA8GA3jTnw53j82PIZMpXO2K7A7oa/aWR/pG0xVkTrMRoSr2WO7woWzhaKgks Qiu5Ef2OtOLcCCthMBik8jre5JgKbOQ296jPDHO+0XBtbeQZ3GCV++0K6RdSFg4RJLtx HI5xt3Zx+SZVy5n98a+5XUHMB8tMTj6+uvbS9OppSt9LaXPRTaCyk7dj/0vpRi483Q/w mMeuWWvg9oj5KISbLCA6AXRIem3Ndp1x+G4RQ0XIlDX39FHUS6Q4CERdEKP9pZtXSvxP St538WJiTpZgiaPMGVu8ypTZGB9xPR5JNHSI92L2DHBK2iYWCB6Zt6EOsYaik2bKgOu7 3ZHQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689329127; x=1691921127; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Q5MF44XQzouX2FbGdHpxAthvfg7u3iO2oNPh/cM8NKE=; b=XI773s93p9OP32+W8E7jfvwyHjwDGWfst6E14EFR7LKLra4ahWiUNJCeEf8cQUY67h vVQ5ThQXwzjQfYBhJ77aZHABazcgIXM9Vb7YYRHfy9zmogbcPUqxJaDqY71Bihv+pilI jxCjxn1tFrFA5RJbCq7FoCn6h2yhUbr2ZUSXwQzm6jfs6WJpFmGarDG6trxSDpWlj3Sf svBRoXIJxs9LF2k9x3FtN3RuIAI/XkCMxgAtdscUuHaZ9mvtRBMzgtioNPc43k0lQCxD mxNMN48C+9zOaClZS1u1sVGETNgbspeMdei31X/q7fSXC5DLLWNq8ZRLlotk40gYOtRg 84cw== X-Gm-Message-State: ABy/qLahyBBulqT7USx4Eu/Akv7nNrtI23KmSItt1fT9fSEoo83w+jVB JHJHfDCdTEdHVd7ciPJIiGkNzWkBteMM0v4= X-Received: from aliceryhl.c.googlers.com ([fda3:e722:ac3:cc00:31:98fb:c0a8:6c8]) (user=aliceryhl job=sendgmr) by 2002:a50:9f07:0:b0:510:dd95:da5d with SMTP id b7-20020a509f07000000b00510dd95da5dmr19412edf.4.1689329127461; Fri, 14 Jul 2023 03:05:27 -0700 (PDT) Date: Fri, 14 Jul 2023 10:05:25 +0000 In-Reply-To: <20230714-rust-time-v2-1-f5aed84218c4@asahilina.net> Mime-Version: 1.0 References: <20230714-rust-time-v2-1-f5aed84218c4@asahilina.net> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog Message-ID: <20230714100525.2192448-1-aliceryhl@google.com> Subject: Re: [PATCH v2] rust: time: New module for timekeeping functions From: Alice Ryhl To: lina@asahilina.net Cc: alex.gaynor@gmail.com, asahi@lists.linux.dev, bjorn3_gh@protonmail.com, boqun.feng@gmail.com, gary@garyguo.net, gbs@canishe.com, heghedus.razvan@protonmail.com, jistone@redhat.com, jstultz@google.com, linux-kernel@vger.kernel.org, ojeda@kernel.org, rust-for-linux@vger.kernel.org, sboyd@kernel.org, tglx@linutronix.de, wedsonaf@gmail.com Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Asahi Lina writes: > +/// Marker trait for clock sources that represent a calendar (wall clock) > +/// relative to the UNIX epoch. > +pub trait WallTime {} What's the purpose of this trait? Perhaps it should have a method to get the UNIX epoch as an Instant? > + /// Returns the time elapsed since an earlier Instant, or > + /// None if the argument is a later Instant. > + pub fn since(&self, earlier: Instant) -> Option { > + if earlier.nanoseconds > self.nanoseconds { > + None > + } else { > + // Casting to u64 and subtracting is guaranteed to give the right > + // result for all inputs, as long as the condition we checked above > + // holds. > + Some(Duration::from_nanos( > + self.nanoseconds as u64 - earlier.nanoseconds as u64, > + )) It looks like you intend to use wrapping semantics for this subtraction so that self=1,earlier=-1 results in a difference of two. In that case, you should explicitly use `.wrapping_sub` instead to convey your intent. I guess you could also use `abs_diff`, which takes two i64s and returns an u64. > +/// Contains the various clock source types available to the kernel. > +pub mod clock { > + use super::*; > + > + /// A clock representing the default kernel time source. > + /// > + /// This is `CLOCK_MONOTONIC` (though it is not the only > + /// monotonic clock) and also the default clock used by > + /// `ktime_get()` in the C API. > + /// > + /// This is like `BootTime`, but does not include time > + /// spent sleeping. > + > + pub struct KernelTime; > + > + impl Clock for KernelTime {} > + impl Monotonic for KernelTime {} > + impl Now for KernelTime { > + fn now() -> Instant { > + Instant::::new(unsafe { bindings::ktime_get() }) > + } > + } We usually add a SAFETY comment even if it is trivial. // SAFETY: Just an FFI call without any safety requirements. Alice