Skip to content

Commit

Permalink
Remove the internal &mut in Requisition
Browse files Browse the repository at this point in the history
Signed-off-by: Nick Cameron <nrc@ncameron.org>
  • Loading branch information
nrc committed Jan 17, 2022
1 parent baaec7e commit fc50395
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions text/0000-dyno.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ struct MyError {
}

impl Error for MyError {
fn provide_context<'a>(&'a self, mut req: Requisition<'a, '_>) {
fn provide_context<'a>(&'a self, req: &mut Requisition<'a>) {
req.provide_ref::<Backtrace>(&self.backtrace)
.provide_ref::<str>(&self.suggestion);
}
Expand Down Expand Up @@ -91,11 +91,11 @@ Lets examine the changes to `Error` required to make this work:
pub mod error {
pub trait Error: Debug + Provider {
...
fn provide_context<'a>(&'a self, _req: Requisition<'a, '_>) {}
fn provide_context<'a>(&'a self, _req: &mut Requisition<'a>) {}
}

impl<T: Error> Provider for T {
fn provide<'a>(&'a self, req: Requisition<'a, '_>) {
fn provide<'a>(&'a self, req: &mut Requisition<'a>) {
self.provide_context(req);
}
}
Expand All @@ -122,14 +122,14 @@ The important parts of `provide_any` are
```rust
pub mod provide_any {
pub trait Provider {
fn provide<'a>(&'a self, req: Requisition<'a, '_>);
fn provide<'a>(&'a self, req: &mut Requisition<'a>);
}

pub fn request_by_type_tag<'a, I: TypeTag<'a>>(provider: &'a dyn Provider) -> Option<I::Type> { ... }

pub struct Requisition<'a, 'b>(...);
pub type Requisition<'a> = ...;

impl<'a, 'b> Requisition<'a, 'b> {
impl<'a> Requisition<'a> {
pub fn provide_ref<T: ?Sized + 'static>(&mut self, value: &'a T) -> &mut Self { ... }
...
}
Expand Down Expand Up @@ -211,7 +211,7 @@ For intermediate libraries, `Value` serves the common case of providing a new or
### Requisition

```rust
impl<'a, 'b> Requisition<'a, 'b> {
impl<'a> Requisition<'a> {
/// Provide a value or other type with only static lifetimes.
pub fn provide_value<T, F>(&mut self, f: F) -> &mut Self
where
Expand Down Expand Up @@ -244,16 +244,14 @@ impl<'a, 'b> Requisition<'a, 'b> {

```rust
impl Error for MyError {
fn provide_context<'a>(&'a self, mut req: Requisition<'a, '_>) {
fn provide_context<'a>(&'a self, req: &mut Requisition<'a>) {
// Taking ownership of the string implies cloning it, so we use `provide_with` to avoid that
// operation unless it is necessary.
req.provide_with::<String>(|| self.suggestion.to_owned());
}
}
```

It seems reasonable that data might be accessed and provided on different threads. For this purpose, `provide_any` includes a version of `Requisition` which implements `Send`: `SendRequisition`. An open question is if it is also useful to support `Sync` variations (and if there is a better name).


# Drawbacks
[drawbacks]: #drawbacks
Expand Down

0 comments on commit fc50395

Please sign in to comment.