Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error-stack: move fmt::HookContext into hook::HookContext #1693

Merged
merged 6 commits into from Dec 20, 2022

Conversation

indietyp
Copy link
Member

@indietyp indietyp commented Dec 18, 2022

🌟 What is the purpose of this PR?

This is in preparation for #1558 and generalizes the HookContext (notably storage) into its own (isolated) type HookContext<T, U>, which is generic over two parameters: T, which are the extra values that might be needed nd U, the current type accessed and used for storage partitioning.

Why is this needed? serde hooks will also need a way to use and store context-sensitive information. I found a lot of code duplication when trying to mirror the storage API, so I chose the consolidation into a single struct. This is purely an internal change and does not change anything for library consumers.

👂 Open Questions

Because cargo-doc considers hook::context::HookContext to be private, we currently do not generate any docs for the new inner type. We would need a way to expose those.

Using cfg(doc) we now expose the struct, which is a bit hacky but enables documentation of all functions.

🔗 Related links

📜 Does this require a change to the docs?

No, purely internal change.

🛡 What tests cover this?

Existing tests.

@github-actions github-actions bot added the area/libs > error-stack Affects the `error-stack` crate (library) label Dec 18, 2022
@codecov
Copy link

codecov bot commented Dec 18, 2022

Codecov Report

Merging #1693 (2918fbd) into main (47a7462) will increase coverage by 1.30%.
The diff coverage is 80.31%.

@@            Coverage Diff             @@
##             main    #1693      +/-   ##
==========================================
+ Coverage   55.95%   57.26%   +1.30%     
==========================================
  Files         223      223              
  Lines       14935    15253     +318     
  Branches      380      380              
==========================================
+ Hits         8357     8734     +377     
+ Misses       6573     6514      -59     
  Partials        5        5              
Flag Coverage Δ
backend-integration-tests 2.87% <ø> (ø)
error-stack 90.87% <80.31%> (-0.02%) ⬇️
unit-tests 1.65% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
packages/libs/error-stack/src/lib.rs 38.46% <ø> (ø)
packages/libs/error-stack/src/hook/context.rs 78.89% <78.89%> (ø)
packages/libs/error-stack/src/fmt/hook.rs 97.91% <86.66%> (+8.17%) ⬆️
packages/libs/error-stack/src/fmt/mod.rs 95.12% <100.00%> (ø)
packages/libs/error-stack/src/hook/mod.rs 100.00% <100.00%> (ø)
..._graph/lib/graph/src/shared/subgraph/edges/edge.rs 10.71% <0.00%> (-2.93%) ⬇️
...graph/lib/graph/src/shared/identifier/knowledge.rs 45.18% <0.00%> (-1.88%) ⬇️
packages/libs/deer/json/src/error.rs 0.00% <0.00%> (ø)
...ges/graph/hash_graph/lib/graph/src/api/rest/mod.rs 0.00% <0.00%> (ø)
... and 12 more

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@vercel
Copy link

vercel bot commented Dec 18, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
hash 🔄 Building (Inspect) Dec 18, 2022 at 2:43PM (UTC)

Copy link
Member

@TimDiekmann TimDiekmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Looks good, happy to approve when CI is green 🎉

Comment on lines +170 to +174
pub fn cast<U>(&mut self) -> &mut HookContext<Extra, U> {
// SAFETY: `HookContext` is marked as repr(transparent) and the changed generic is only used
// inside of the `PhantomData`
unsafe { &mut *(self as *mut Self).cast::<HookContext<Extra, U>>() }
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I take the opportunity to ask this question as we touched this code: Do you see a way to remove the unsafe here? I know, why we have, I'm just wondering if there is a way to avoid it (this question is unrelated to this PR).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so, if we would instead transmute mut self instead we could use RFC 2528 may be of help.

@indietyp indietyp enabled auto-merge (squash) December 20, 2022 10:31
@indietyp indietyp merged commit b02ee5c into main Dec 20, 2022
@indietyp indietyp deleted the bm/es/hook-context-unify branch December 20, 2022 10:52
trunk-io bot pushed a commit that referenced this pull request Jan 31, 2023
## 🌟 What is the purpose of this PR?

#1693 separated `fmt::HookContext` into `hook::HookContext` and `fmt::HookContext`, so that it can be used in #1558. While working, the problem is that documentation doesn't render correctly because `hook::HookContext` is private in public. Therefore unreachable rustdoc is unable to navigate to the type, and the documentation is incomplete.

This PR moves to a macro-based approach to enable better documentation. Instead of using an unreachable public type, we have a macro that implements the necessary common functions. This is by far not ideal, but I found this to be the best way without compromising #1558 or future hooks.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/libs > error-stack Affects the `error-stack` crate (library)
Development

Successfully merging this pull request may close these issues.

None yet

2 participants