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

Make AsyncResponseListener methods take `&mut self` #8043

Merged
merged 2 commits into from Oct 16, 2015
Merged
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Next

Make AsyncResponseListener methods take `&mut self`.

  • Loading branch information
eefriedman committed Oct 15, 2015
commit c1aff0b678d346e5f782e06a3e1e512128fa3f05
@@ -163,13 +163,13 @@ pub trait AsyncFetchListener {
/// A listener for asynchronous network events. Cancelling the underlying request is unsupported.
pub trait AsyncResponseListener {
/// The response headers for a request have been received.
fn headers_available(&self, metadata: Metadata);
fn headers_available(&mut self, metadata: Metadata);
/// A portion of the response body has been received. This data is unavailable after
/// this method returned, and must be stored accordingly.
fn data_available(&self, payload: Vec<u8>);
fn data_available(&mut self, payload: Vec<u8>);
/// The response is complete. If the provided status is an Err value, there is no guarantee
/// that the response body was completely read.
fn response_complete(&self, status: Result<(), String>);
fn response_complete(&mut self, status: Result<(), String>);
}

/// Data for passing between threads/processes to indicate a particular action to
@@ -186,7 +186,7 @@ pub enum ResponseAction {

impl ResponseAction {
/// Execute the default action on a provided listener.
pub fn process(self, listener: &AsyncResponseListener) {
pub fn process(self, listener: &mut AsyncResponseListener) {
match self {
ResponseAction::HeadersAvailable(m) => listener.headers_available(m),
ResponseAction::DataAvailable(d) => listener.data_available(d),
@@ -112,13 +112,13 @@ impl CORSRequest {
// This is shoe-horning the CORSReponse stuff into the rest of the async network
// framework right now. It would be worth redesigning http_fetch to do this properly.
impl AsyncResponseListener for CORSContext {
fn headers_available(&self, _metadata: Metadata) {
fn headers_available(&mut self, _metadata: Metadata) {
}

fn data_available(&self, _payload: Vec<u8>) {
fn data_available(&mut self, _payload: Vec<u8>) {
}

fn response_complete(&self, _status: Result<(), String>) {
fn response_complete(&mut self, _status: Result<(), String>) {
let response = self.response.borrow_mut().take().unwrap();
self.listener.response_available(response);
}
@@ -138,16 +138,16 @@ struct ScriptContext {
}

impl AsyncResponseListener for ScriptContext {
fn headers_available(&self, metadata: Metadata) {
fn headers_available(&mut self, metadata: Metadata) {
*self.metadata.borrow_mut() = Some(metadata);
}

fn data_available(&self, payload: Vec<u8>) {
fn data_available(&mut self, payload: Vec<u8>) {
let mut payload = payload;
self.data.borrow_mut().append(&mut payload);
}

fn response_complete(&self, status: Result<(), String>) {
fn response_complete(&mut self, status: Result<(), String>) {
let load = status.map(|_| {
let data = mem::replace(&mut *self.data.borrow_mut(), vec!());
let metadata = self.metadata.borrow_mut().take().unwrap();
@@ -97,7 +97,7 @@ impl ParserContext {
}

impl AsyncResponseListener for ParserContext {
fn headers_available(&self, metadata: Metadata) {
fn headers_available(&mut self, metadata: Metadata) {
let content_type = metadata.content_type.clone();

let parser = ScriptTask::page_fetch_complete(self.id.clone(), self.subpage.clone(),
@@ -137,7 +137,7 @@ impl AsyncResponseListener for ParserContext {
}
}

fn data_available(&self, payload: Vec<u8>) {
fn data_available(&mut self, payload: Vec<u8>) {
if !self.is_image_document.get() {
// FIXME: use Vec<u8> (html5ever #34)
let data = UTF_8.decode(&payload, DecoderTrap::Replace).unwrap();
@@ -149,7 +149,7 @@ impl AsyncResponseListener for ParserContext {
}
}

fn response_complete(&self, status: Result<(), String>) {
fn response_complete(&mut self, status: Result<(), String>) {
let parser = match self.parser.borrow().as_ref() {
Some(parser) => parser.root(),
None => return,
@@ -237,7 +237,7 @@ impl XMLHttpRequest {
resource_task: ResourceTask,
load_data: LoadData) {
impl AsyncResponseListener for XHRContext {
fn headers_available(&self, metadata: Metadata) {
fn headers_available(&mut self, metadata: Metadata) {
let xhr = self.xhr.root();
let rv = xhr.r().process_headers_available(self.cors_request.clone(),
self.gen_id,
@@ -247,13 +247,13 @@ impl XMLHttpRequest {
}
}

fn data_available(&self, payload: Vec<u8>) {
fn data_available(&mut self, payload: Vec<u8>) {
self.buf.borrow_mut().push_all(&payload);
let xhr = self.xhr.root();
xhr.r().process_data_available(self.gen_id, self.buf.borrow().clone());
}

fn response_complete(&self, status: Result<(), String>) {
fn response_complete(&mut self, status: Result<(), String>) {
let xhr = self.xhr.root();
let rv = xhr.r().process_response_complete(self.gen_id, status);
*self.sync_status.borrow_mut() = Some(rv);
@@ -43,9 +43,9 @@ struct ListenerRunnable<T: AsyncResponseListener + PreInvoke + Send> {
impl<T: AsyncResponseListener + PreInvoke + Send> Runnable for ListenerRunnable<T> {
fn handler(self: Box<ListenerRunnable<T>>) {
let this = *self;
let context = this.context.lock().unwrap();
let mut context = this.context.lock().unwrap();
if context.should_invoke() {
this.action.process(&*context);
this.action.process(&mut *context);
}
}
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.