Skip to content

Commit c8c9753

Browse files
authored
Use alloc, core when possible (TraceMachina#1704)
This changes uses of std to be core or alloc as appropriate whenever possible. Such usage is enforced by clippy. The unrelated changes to Cargo.toml are a result of formatting.
1 parent 8a0d803 commit c8c9753

File tree

104 files changed

+517
-467
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

104 files changed

+517
-467
lines changed

.bazelrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ build --@rules_rust//:extra_rustc_flag=-Wunused_import_braces
9494
build --@rules_rust//:extra_rustc_flag=-Wunused_lifetimes
9595
build --@rules_rust//:extra_rustc_flag=-Wunused_qualifications
9696
build --@rules_rust//:extra_rustc_flag=-Wvariant_size_differences
97+
build --@rules_rust//:clippy_flag=-Dclippy::alloc_instead_of_core
98+
build --@rules_rust//:clippy_flag=-Dclippy::std_instead_of_core
9799
# TODO(aaronmondal): Extend these flags until we can run with clippy::pedantic.
98100
build --@rules_rust//:clippy_flags=-Dwarnings,-Dclippy::missing_const_for_fn,-Dclippy::uninlined_format_args,-Dclippy::manual_string_new,-Dclippy::manual_let_else,-Dclippy::single_match_else,-Dclippy::redundant_closure_for_method_calls,-Dclippy::semicolon_if_nothing_returned,-Dclippy::unreadable_literal,-Dclippy::range_plus_one,-Dclippy::inconsistent_struct_constructor,-Dclippy::match_wildcard_for_single_variants,-Dclippy::implicit_clone,-Dclippy::needless_pass_by_value,-Dclippy::explicit_deref_methods,-Dclippy::trivially_copy_pass_by_ref,-Dclippy::unnecessary_wraps,-Dclippy::cast_lossless,-Dclippy::map_unwrap_or,-Dclippy::ref_as_ptr,-Dclippy::inline_always,-Dclippy::redundant_else,-Dclippy::return_self_not_must_use,-Dclippy::match_same_arms,-Dclippy::explicit_iter_loop,-Dclippy::items_after_statements,-Dclippy::explicit_into_iter_loop,-Dclippy::stable_sort_primitive,-Dclippy::ptr_as_ptr,-Dclippy::needless_raw_string_hashes,-Dclippy::default_trait_access,-Dclippy::ignored_unit_patterns,-Dclippy::needless_continue,-Dclippy::wildcard_imports,-Dclippy::doc_markdown,-Dclippy::struct_field_names,-Dclippy::implicit_hasher
99101
build --@rules_rust//:clippy.toml=//:clippy.toml

Cargo.toml

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ debug = false
2222
name = "nativelink"
2323

2424
[features]
25-
nix = [
26-
"nativelink-worker/nix"
27-
]
25+
nix = ["nativelink-worker/nix"]
2826

2927
[dependencies]
3028
nativelink-error = { path = "nativelink-error" }
@@ -44,14 +42,24 @@ hyper = "1.6.0"
4442
hyper-util = "0.1.11"
4543
mimalloc = "0.1.44"
4644
parking_lot = "0.12.3"
47-
rustls-pemfile = { version = "2.2.0", features = ["std"], default-features = false }
45+
rustls-pemfile = { version = "2.2.0", features = [
46+
"std",
47+
], default-features = false }
4848
scopeguard = { version = "1.2.0", default-features = false }
4949
serde_json5 = "0.2.1"
50-
tokio = { version = "1.44.1", features = ["fs", "rt-multi-thread", "signal", "io-util"], default-features = false }
50+
tokio = { version = "1.44.1", features = [
51+
"fs",
52+
"rt-multi-thread",
53+
"signal",
54+
"io-util",
55+
], default-features = false }
5156
tokio-rustls = { version = "0.26.2", default-features = false, features = [
5257
"ring",
5358
] }
54-
tonic = { version = "0.13.0", features = ["transport", "tls-ring"], default-features = false }
59+
tonic = { version = "0.13.0", features = [
60+
"transport",
61+
"tls-ring",
62+
], default-features = false }
5563
tower = { version = "0.5.2", default-features = false }
5664
tracing = { version = "0.1.41", default-features = false }
5765
opentelemetry_sdk = { version = "0.29.0", default-features = false }
@@ -117,4 +125,7 @@ unused-qualifications = "warn"
117125
variant-size-differences = "warn"
118126

119127
[workspace.lints.clippy]
128+
alloc-instead-of-core = "deny"
129+
std-instead-of-core = "deny"
130+
120131
all = { level = "warn", priority = -1 }

nativelink-config/src/serde_utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
use core::marker::PhantomData;
1516
use std::borrow::Cow;
1617
use std::fmt;
17-
use std::marker::PhantomData;
1818

1919
use byte_unit::Byte;
2020
use humantime::parse_duration;

nativelink-error/src/lib.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use std::convert::Into;
15+
use core::convert::Into;
1616

1717
use nativelink_metric::{
1818
MetricFieldData, MetricKind, MetricPublishKnownKindData, MetricsComponent,
@@ -116,7 +116,7 @@ impl Error {
116116
}
117117
}
118118

119-
impl std::error::Error for Error {}
119+
impl core::error::Error for Error {}
120120

121121
impl From<Error> for nativelink_proto::google::rpc::Status {
122122
fn from(val: Error) -> Self {
@@ -137,8 +137,8 @@ impl From<nativelink_proto::google::rpc::Status> for Error {
137137
}
138138
}
139139

140-
impl std::fmt::Display for Error {
141-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
140+
impl core::fmt::Display for Error {
141+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
142142
// A manual impl to reduce the noise of frequently empty fields.
143143
let mut builder = f.debug_struct("Error");
144144

@@ -170,8 +170,8 @@ impl From<prost::UnknownEnumValue> for Error {
170170
}
171171
}
172172

173-
impl From<std::num::TryFromIntError> for Error {
174-
fn from(err: std::num::TryFromIntError) -> Self {
173+
impl From<core::num::TryFromIntError> for Error {
174+
fn from(err: core::num::TryFromIntError) -> Self {
175175
make_err!(Code::InvalidArgument, "{}", err.to_string())
176176
}
177177
}
@@ -182,14 +182,14 @@ impl From<tokio::task::JoinError> for Error {
182182
}
183183
}
184184

185-
impl From<std::num::ParseIntError> for Error {
186-
fn from(err: std::num::ParseIntError) -> Self {
185+
impl From<core::num::ParseIntError> for Error {
186+
fn from(err: core::num::ParseIntError) -> Self {
187187
make_err!(Code::InvalidArgument, "{}", err.to_string())
188188
}
189189
}
190190

191-
impl From<std::convert::Infallible> for Error {
192-
fn from(_err: std::convert::Infallible) -> Self {
191+
impl From<core::convert::Infallible> for Error {
192+
fn from(_err: core::convert::Infallible) -> Self {
193193
// Infallible is an error type that can never happen.
194194
unreachable!();
195195
}

nativelink-metric-collector/src/metrics_collection.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
use core::ops::{Deref, DerefMut};
1516
use std::borrow::Cow;
1617
use std::collections::HashMap;
17-
use std::ops::{Deref, DerefMut};
1818

1919
use serde::Serialize;
2020

nativelink-metric-collector/src/metrics_visitors.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
use core::fmt::Debug;
1516
use std::borrow::Cow;
16-
use std::fmt::Debug;
1717

1818
use nativelink_metric::MetricKind;
1919
use serde::Serialize;
@@ -140,7 +140,7 @@ impl Visit for MetricDataVisitor {
140140
field => panic!("UNKNOWN FIELD {field}"),
141141
}
142142
}
143-
fn record_error(&mut self, _field: &Field, _value: &(dyn std::error::Error + 'static)) {}
143+
fn record_error(&mut self, _field: &Field, _value: &(dyn core::error::Error + 'static)) {}
144144
}
145145

146146
/// An intermediate structed that will have it's contents populated

nativelink-metric-collector/src/tracing_layers.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
use core::fmt::Debug;
16+
use core::marker::PhantomData;
1517
use std::borrow::Cow;
1618
use std::collections::HashMap;
17-
use std::fmt::Debug;
18-
use std::marker::PhantomData;
1919
use std::sync::Arc;
2020

2121
use parking_lot::Mutex;

nativelink-metric-collector/tests/metric_collector_test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
use core::fmt::Debug;
16+
use core::marker::PhantomData;
1517
use std::collections::HashMap;
16-
use std::fmt::Debug;
17-
use std::marker::PhantomData;
1818

1919
use nativelink_metric::{MetricFieldData, MetricKind, MetricsComponent};
2020
use nativelink_metric_collector::MetricsCollectorLayer;

nativelink-metric/src/lib.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
use core::hash::BuildHasher;
16+
use core::sync::atomic::{AtomicI64, AtomicU64, Ordering};
17+
use core::time::Duration;
1518
use std::borrow::Cow;
1619
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
17-
use std::hash::BuildHasher;
18-
use std::sync::atomic::{AtomicI64, AtomicU64, Ordering};
1920
use std::sync::{Arc, Weak};
20-
use std::time::{Duration, SystemTime, UNIX_EPOCH};
21+
use std::time::{SystemTime, UNIX_EPOCH};
2122

2223
pub use nativelink_metric_macro_derive::MetricsComponent;
2324
pub use tracing::{error as __metric_error, info as __metric_event, info_span as __metric_span};
@@ -29,13 +30,13 @@ pub use tracing::{error as __metric_error, info as __metric_event, info_span as
2930
#[derive(Debug)]
3031
pub struct Error(String);
3132

32-
impl std::fmt::Display for Error {
33-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33+
impl core::fmt::Display for Error {
34+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
3435
write!(f, "{}", self.0)
3536
}
3637
}
3738

38-
impl std::error::Error for Error {}
39+
impl core::error::Error for Error {}
3940

4041
/// Holds metadata about the field that is being published.
4142
#[derive(Debug, Default, Clone)]

nativelink-proto/gen_lib_rs_tool.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,14 @@
3939
#![allow(
4040
unknown_lints,
4141
unused_qualifications,
42+
clippy::alloc_instead_of_core,
4243
clippy::default_trait_access,
4344
clippy::doc_lazy_continuation,
4445
clippy::doc_markdown,
46+
clippy::doc_overindented_list_items,
4547
clippy::large_enum_variant,
4648
clippy::missing_const_for_fn,
47-
clippy::doc_overindented_list_items,
49+
clippy::std_instead_of_core,
4850
rustdoc::invalid_html_tags
4951
)]
5052
"""

0 commit comments

Comments
 (0)