Implement `slice(&self, range: impl RangeBounds<usize>)` for `HeaderValue`
まだ誰も着手していません。
評価
調査の方向性
まず HeaderValue の既存の共有ストレージ API を確認し、次に Bytes::slice と issue #459 で議論されている代替案を調査します。maintainer がどのインターフェースを求めているかを明確にし、範囲の挙動と所有権への影響を検討します。選択した API が適切なカバレッジとドキュメントを伴って実装されれば、作業は完了です。
索引モデルが issue の本文から書いたものです。
説明
NB. I initially opened this issue suggesting implementing std::ops::Index instead of a slice method. That original proposal wouldn't work because the trait would always returns a &HeaderValue (a &<Self as Index>::Output) and I'm proposing a method that returns an owned HeaderValue.
My motivation for this is to simplify handling list-based header values in a way that avoids copying the HeaderValue's data. Consider If-None-Match, which may contain multiple comma-separated entity-tags, or Cookies which may contain multiple semicolon-separated cookie-pairs. In both cases, it's necessary to operate on individual values within the list, and sub-slicing is a natural choice;
let inm = HeaderValue::from_static(
r#"If-None-Match: W/"67ab43", "54ed21", "7892dd""#,
);
assert_eq!(&inm.as_bytes()[15 .. 25], br#"W/"67ab43""#);
assert_eq!(&inm.as_bytes()[27 .. 35], br#""54ed21""#);
assert_eq!(&inm.as_bytes()[37 .. 45], br#""7892dd""#);
The above works well if 1. you can limit operations to &[u8]s and 2. you can manage the lifetime of the borrowed-from HeaderValue. Both of those limitations seem unnecessary given HeaderValue's Arc-like memory characteristic, so it's more natural to my mind to allow something like the below,
let inm = HeaderValue::from_static(
r#"If-None-Match: W/"67ab43", "54ed21", "7892dd""#,
);
assert_eq!(inm.slice(15 .. 25), HeaderValue::from_static(r#"W/"67ab43""#));
assert_eq!(inm.slice(27 .. 35), HeaderValue::from_static(r#""54ed21""#));
assert_eq!(inm.slice(37 .. 45), HeaderValue::from_static(r#""7892dd""#));
This would be a relatively simple change to make using Bytes::slice to do the heavy lifting.
I haven't put together a PR for the above yet because I see two open questions that I feel a maintainer may want to weigh in on.
First is whether or not this should be done at all. Returning a HeaderValue from a method named slice (rather than a &HeaderValue) may be surprising in the broader context. It would also implicitly codify the use of Bytes — or some other Arc-like memory management system — in the type's interface. I don't see either of these as blockers, given from_maybe_shared is precedent for both, and HeaderValue has been backed by Bytes for years now.
Second, and maybe more interesting, is whether this kind of interface would be better or worse than directly exposing the underlying Bytes object as per #459. Rather than offering a HeaderValue::slice method, consumers could directly call Bytes::slice, e.g.
let inm = HeaderValue::from_static(
r#"If-None-Match: W/"67ab43", "54ed21", "7892dd""#,
);
let b_inm: Bytes = inm.as_shared();
assert_eq!(b_inm.slice(15 .. 25), Bytes::from_static(r#"W/"67ab43""#));
assert_eq!(b_inm.slice(27 .. 35), Bytes::from_static(r#""54ed21""#));
assert_eq!(b_inm.slice(37 .. 45), Bytes::from_static(r#""7892dd""#));
I'd be happy to put up a PR for either change.
- 主要言語
- Rust
- スター
- 1.4k
- フォーク
- 378
- 平均マージ
- 1日 21時間
- マージ済み PR(30日)
- 5
コントリビューションガイド
このリポジトリのコントリビューションガイドは索引されていません
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
hyperium/http のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
-
難易度 4/5 3〜5日 初心者へのやさしさ 45/100
-
難易度 3/5 1〜2日 初心者へのやさしさ 62/100
-
難易度 3/5 1〜2日 初心者へのやさしさ 58/100
-
難易度 3/5 1〜2日 初心者へのやさしさ 62/100
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
-
state:needs triage
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
zed-industries/zed#64680 · コメント 2 件 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
RustPython/RustPython#8802 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
TheLarkInn/aipm#2390 ·