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

Thread local fallback #146

Merged
merged 3 commits into from
Oct 17, 2019
Merged

Conversation

divergentdave
Copy link
Contributor

This PR provides a failing test to reproduce the issue described in #145, and fixes the issue as described by handling the AccessError.

@KodrAus
Copy link
Collaborator

KodrAus commented Oct 17, 2019

Thanks for the PR @divergentdave!

It looks like we're accumulating a bit of logic in this method so maybe it's time to refactor it a bit into something more manageable. I was thinking we could just inline the write call into the places where we have a Formatter. So something like (probably doesn't actually compile):

let print = |formatter, record| {
    let _ = (self.format)(&mut formatter, record)
        .and_then(|_| formatter.print(&self.writer));
    formatter.clear();
}

let printed = FORMATTER.try_with(|tl_buf| {
    match tl_buf.try_borrow_mut() {
        // There are no active borrows of the buffer
        Ok(ref mut tl_buf) => match tl_buf {
            // We have a previously set formatter
            Some(ref mut formatter) => {
                if formatter.write_style() != self.writer.write_style() {
                    *formatter = Formatter::new(&self.writer)
                }

                print(formatter, record);
            },
            // We don't have a previously set formatter
            None => {
                let mut formatter = Formatter::new(&self.writer);
                print(formatter, record);

                *tl_buf = Some(formatter);
            }
        }
        // There's already an active borrow of the buffer
        Err(_) => {
            print(&mut Formatter::new(&self.writer), record);
        }
    }
}).is_ok();

if !printed {
    print(&mut Formatter::new(&self.writer), record);
}

What do you think?

@divergentdave
Copy link
Contributor Author

Sounds good, I'll split that out

Copy link
Collaborator

@KodrAus KodrAus left a comment

Choose a reason for hiding this comment

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

Thanks @divergentdave! This looks good to me

@KodrAus KodrAus merged commit 87496cf into rust-cli:master Oct 17, 2019
@divergentdave divergentdave deleted the thread-local-fallback branch March 28, 2020 17:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants