Summary

Expand the traits implemented

実装する
by structs
構造、構造体
libc crate to include Debug, Eq, Hash, and PartialEq.

Motivation

This will allow

許可する、可能にする
downstream crates to easily support similar
似ている、同様の
operations
演算、操作
with any types they provide
与える
that contain
含む
libc structs.
構造、構造体
Additionally The Rust API Guidelines specify
特定する、指定する、規定する
that it is considered
みなす、考慮する
useful to expose as many traits as possible from the standard library. In order
順序
to facilitate the following of these guidelines, official Rust libraries should lead by example.

For many of these traits, it is trivial for downstream crates to implement

実装する
them for these types by using newtype wrappers. As a specific
特定の
example, the nix crate offers the TimeSpec wrapper type around the timespec struct.
構造、構造体
This wrapper could easily implement
実装する
Eq through comparing
比較する
both fields in the struct.
構造、構造体

Unfortunately there are a great many structs

構造、構造体
that are large and vary widely between platforms. Some of these in use by nix are dqblk, utsname, and statvfs. These structs
構造、構造体
have fields and field types that vary across platforms. As nix aims to support as many platforms as libc does, this variation makes implementing
実装する
these traits manually on wrapper types time consuming and error prone.

Guide-level explanation

Add an extra_traits feature to the libc library that enables Debug, Eq, Hash, and PartialEq implementations

実装
for all structs.
構造、構造体

Reference-level explanation

The Debug, Eq/PartialEq, and Hash traits will be added

たす
as automatic
自動の
derives within the s! macro in src/macros.rs if the corresponding
対応する
feature flag is enabled. This won't work for some types because auto-derive doesn't work for arrays
配列
larger than 32 elements,
要素
so for these they'll be implemented
実装する
manually. For libc as of bbda50d20937e570df5ec857eea0e2a098e76b2d on x86_64-unknown-linux-gnu these many structs
構造、構造体
will need manual
マニュアル、手動
implementations:
実装

  • Debug - 17
  • Eq/PartialEq - 46
  • Hash - 17

Drawbacks

While most structs

構造、構造体
will be able to derive
派生する
these implementations
実装
automatically,
自動的に
some will not (for example arrays
配列
larger than 32 elements). This will make it harder to add some structs
構造、構造体
to libc.

This extra trait will increase the testing requirements for libc.

Rationale and alternatives
代わりのもの、選択肢

Adding

たす
these trait implementations
実装
behind a singular feature flag has the best compination of utility and ergonomics out of the possible alternatives
代わりのもの、選択肢
listed
リスト、列挙する
below:

Always enabled with no feature flags

This was regarded as unsuitable because it increases

増加する、昇順の
compilation times by 100-200%. Compilation times of libc was tested at commit bbda50d20937e570df5ec857eea0e2a098e76b2d with modifications to add derives for the traits discussed here under the extra_traits feature (with no other features). Some types failed to have these traits derived because of specific
特定の
fields, so these were removed from the struct
構造、構造体
declaration.
宣言
The table below shows the compilation times:

Build argumentsTime
cargo clean && cargo build --no-default-features0.84s
cargo clean && cargo build --no-default-features --features extra_traits2.17s
cargo clean && cargo build --no-default-features --release0.64s
cargo clean && cargo build --no-default-features --release --features extra_traits1.80s
cargo clean && cargo build --no-default-features --features use_std1.14s
cargo clean && cargo build --no-default-features --features use_std,extra_traits2.34s
cargo clean && cargo build --no-default-features --release --features use_std0.66s
cargo clean && cargo build --no-default-features --release --features use_std,extra_traits1.94s

Default-on feature

For crates that are more than one level above libc in the dependency

依存、依存関係
chain it will be impossible for them to opt out. This could also happen with a default-off feature flag, but it's more likely the library authors will expose it as a flag as well.

Multiple
複数の
feature flags

Instead of having a single

単一の
extra_traits feature, have it and feature flags for each trait individually
個々に
like:

  • trait_debug - Enables Debug for all structs
    構造、構造体
  • trait_eg - Enables Eq and PartialEq for all structs
    構造、構造体
  • trait_hash - Enables Hash for all structs
    構造、構造体
  • extra_traits - Enables all of the above through dependent features

This change should reduce compilation times when not all traits are desired. The downsides are that it complicates CI. It can be added

たす
in a backwards-compatible manner later should compilation times or consumer demand changes.

Unresolved questions