TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Contexts and Capabilities in Rust

43 pointsby tmandryover 3 years ago

4 comments

ncmncmover 3 years ago
This sort of facility is the reason why C++ iostreams (technically, std::ios_base) support a primitive array type and static index allocator, via members iword&#x2F;pword and xalloc. It is not often used, but where used it would be hard to do anything else.<p><a href="https:&#x2F;&#x2F;en.cppreference.com&#x2F;w&#x2F;cpp&#x2F;io&#x2F;ios_base" rel="nofollow">https:&#x2F;&#x2F;en.cppreference.com&#x2F;w&#x2F;cpp&#x2F;io&#x2F;ios_base</a><p>It is quite common for a stream object to thread a call chain from top to bottom, for independent reasons. So, ability to stick on other context has saved bacon fairly often, over the decades.
评论 #29645933 未加载
amituover 3 years ago
This proposal addresses a problem that I have faced all the time. I used to use Golang before Rust, Golang uses `context` package for this, and I have created `In` objects to emulate context. Consider this code[1], I pass it all over the place. I never liked it, it made function signatures complex, and did not always solve it, I had to keep track of more than one &quot;context&quot; types as different parts of code need different capabilities.<p>So I am glad people are working on it. I also find the solution quite ingenious.<p>The main concern that came to my mind was aliasing. This is like &quot;spooky argument at a distance&quot; kind of thing happening, and how do I &quot;rename&quot; these arguments in transit?<p>Say I am going to call two methods, both of them require different context data but with same &quot;thing&quot;, eg say I want to implement a custom deserialiser that deserialises into same type, `Vec&lt;String&gt;`, but instead of allocating new ones I want to deserialise them into two different pre allocated buffers which I want to pass as context. But each call must get a different buffer.<p>Of course things would be easy if they were clear different `with` clauses, but if they overlap then I am not sure.<p>[1]: <a href="https:&#x2F;&#x2F;github.com&#x2F;FifthTry&#x2F;realm&#x2F;blob&#x2F;master&#x2F;src&#x2F;urls.rs#L15-L22" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;FifthTry&#x2F;realm&#x2F;blob&#x2F;master&#x2F;src&#x2F;urls.rs#L1...</a>
saghmover 3 years ago
I might just not be understanding what&#x27;s special about this keyword, but my instinct when I see this sort of thing is to wonder if a proc macro could get us most of the way there. Obviously that isn&#x27;t possible in some cases, and extra syntax is needed (e.g. the `await` keyword), but this at least feels like something that syntax-wise would probably fit pretty well as an annotation on a function, e.g.<p><pre><code> #[context = &quot;foo&quot;] fn bar() { foo.something } </code></pre> If a proc macro couldn&#x27;t provide this functionality, I still think I&#x27;d prefer it being some sort of top-level attribute rather than a keyword, mostly due to the fact that it&#x27;s visible without having to look into the function, which makes it easier to notice due to visual scanning or when using code folding to hide the bodies of methods by default. Obviously this is mostly a personal preference, but I have a pretty strongly but somewhat vague sentiment that all the info needed to understand the scope of a function should appear in the signature (or right above, in the case of annotations) rather than inside.
dataangelover 3 years ago
A few thoughts:<p>* I strongly recommend to comparing against Racket&#x27;s parameters which approach the same problem.<p>* It&#x27;s a little weird that the solution to needing to repetitively pass arguments requires you to write a declaration that is just as much work as declaring a regular argument. You still have at the call sites, but meh.<p>* &quot;Has zero runtime overhead; it compiles down to the code you would write anyway.&quot; My code would use global variables to avoid wasting registers to pass arguments that are used everywhere. But I&#x27;m guessing the code assumed here is the kind bloating every function call with more arguments? How does this look when Rust exposes a C ABI function with a with clause?