Relax the requirement of Atom::with. r?Manishearth draft
authorXidorn Quan <me@upsuper.org>
Mon, 15 May 2017 14:19:02 +1000
changeset 577632 5adc4a1fa7cc6b132794d3dd82dfe95a4f5a5690
parent 577631 a7c0971b77dd25af6e8c68b5133f01376681d548
child 577633 2674e1f440ba95bf70c42642d4569a98d3fc7106
push id58742
push userxquan@mozilla.com
push dateMon, 15 May 2017 05:03:58 +0000
reviewersManishearth
milestone55.0a1
Relax the requirement of Atom::with. r?Manishearth This commit does two things: 1. change the type of callback from "&mut FnMut" to "FnOnce" 2. remove the 'static lifetime restriction for the return type The second change is necessary for the next commit. To be honest, I don't fully understand how closures work. But it seems the change here just works, and looks reasonable to me. I simply trust that rustc has confirmed the code is doing the right thing. MozReview-Commit-ID: 1RqLWlxv7wf
servo/components/style/gecko/snapshot_helpers.rs
servo/components/style/gecko_string_cache/mod.rs
servo/components/style/properties/longhand/font.mako.rs
servo/ports/geckolib/glue.rs
--- a/servo/components/style/gecko/snapshot_helpers.rs
+++ b/servo/components/style/gecko/snapshot_helpers.rs
@@ -42,17 +42,17 @@ pub fn each_class<F, T>(item: T,
     where F: FnMut(&Atom)
 {
     unsafe {
         let mut class: *mut nsIAtom = ptr::null_mut();
         let mut list: *mut *mut nsIAtom = ptr::null_mut();
         let length = getter(item, &mut class, &mut list);
         match length {
             0 => {}
-            1 => Atom::with(class, &mut callback),
+            1 => Atom::with(class, callback),
             n => {
                 let classes = slice::from_raw_parts(list, n as usize);
                 for c in classes {
                     Atom::with(*c, &mut callback)
                 }
             }
         }
     }
--- a/servo/components/style/gecko_string_cache/mod.rs
+++ b/servo/components/style/gecko_string_cache/mod.rs
@@ -193,17 +193,17 @@ impl fmt::Display for WeakAtom {
             try!(w.write_char(c.unwrap_or(char::REPLACEMENT_CHARACTER)))
         }
         Ok(())
     }
 }
 
 impl Atom {
     /// Execute a callback with the atom represented by `ptr`.
-    pub unsafe fn with<F, R: 'static>(ptr: *mut nsIAtom, callback: &mut F) -> R where F: FnMut(&Atom) -> R {
+    pub unsafe fn with<F, R>(ptr: *mut nsIAtom, callback: F) -> R where F: FnOnce(&Atom) -> R {
         let atom = Atom(WeakAtom::new(ptr));
         let ret = callback(&atom);
         mem::forget(atom);
         ret
     }
 
     /// Creates an atom from an static atom pointer without checking in release
     /// builds.
--- a/servo/components/style/properties/longhand/font.mako.rs
+++ b/servo/components/style/properties/longhand/font.mako.rs
@@ -716,17 +716,17 @@
                     [9,   10,    13,    16,    18,    24,    32,    48]
                 ];
 
                 static FONT_SIZE_FACTORS: [i32; 8] = [60, 75, 89, 100, 120, 150, 200, 300];
 
                 // XXXManishearth handle quirks mode
 
                 let ref gecko_font = cx.style().get_font().gecko();
-                let base_size = unsafe { Atom::with(gecko_font.mLanguage.raw::<nsIAtom>(), &mut |atom| {
+                let base_size = unsafe { Atom::with(gecko_font.mLanguage.raw::<nsIAtom>(), |atom| {
                     cx.font_metrics_provider.get_size(atom, gecko_font.mGenericID).0
                 }) };
 
                 let base_size_px = au_to_int_px(base_size as f32);
                 let html_size = *self as usize;
                 if base_size_px >= 9 && base_size_px <= 16 {
                     Au::from_px(FONT_SIZE_MAPPING[(base_size_px - 9) as usize][html_size])
                 } else {
--- a/servo/ports/geckolib/glue.rs
+++ b/servo/ports/geckolib/glue.rs
@@ -2426,17 +2426,17 @@ pub extern "C" fn Servo_StyleSet_Resolve
                                               parent_style,
                                               declarations.clone()).into_strong()
 }
 
 #[no_mangle]
 pub extern "C" fn Servo_StyleSet_MightHaveAttributeDependency(raw_data: RawServoStyleSetBorrowed,
                                                               local_name: *mut nsIAtom) -> bool {
     let data = PerDocumentStyleData::from_ffi(raw_data).borrow();
-    unsafe { Atom::with(local_name, &mut |atom| data.stylist.might_have_attribute_dependency(atom)) }
+    unsafe { Atom::with(local_name, |atom| data.stylist.might_have_attribute_dependency(atom)) }
 }
 
 #[no_mangle]
 pub extern "C" fn Servo_StyleSet_HasStateDependency(raw_data: RawServoStyleSetBorrowed,
                                                     state: u64) -> bool {
     let data = PerDocumentStyleData::from_ffi(raw_data).borrow();
     data.stylist.has_state_dependency(ElementState::from_bits_truncate(state))
 }