Use safe NonZero constructor instead of an explicit null check

This commit is contained in:
Simon Sapin 2018-01-22 13:26:04 +01:00
parent 10ec5a2bb0
commit b78ac6ba6a
4 changed files with 6 additions and 10 deletions

View file

@ -105,8 +105,7 @@ impl<T: DomObject + JSTraceable + Iterable> IterableIterator<T> {
};
self.index.set(index + 1);
result.map(|_| {
assert!(!rval.is_null());
unsafe { NonNull::new_unchecked(rval.get()) }
NonNull::new(rval.get()).expect("got a null pointer")
})
}
}

View file

@ -160,7 +160,6 @@ impl ImageDataMethods for ImageData {
#[allow(unsafe_code)]
// https://html.spec.whatwg.org/multipage/#dom-imagedata-data
unsafe fn Data(&self, _: *mut JSContext) -> NonNull<JSObject> {
assert!(!self.data.get().is_null());
NonNull::new_unchecked(self.data.get())
NonNull::new(self.data.get()).expect("got a null pointer")
}
}

View file

@ -154,8 +154,7 @@ impl TestBindingMethods for TestBinding {
#[allow(unsafe_code)]
unsafe fn ArrayAttribute(&self, cx: *mut JSContext) -> NonNull<JSObject> {
rooted!(in(cx) let array = JS_NewUint8ClampedArray(cx, 16));
assert!(!array.is_null());
NonNull::new_unchecked(array.get())
NonNull::new(array.get()).expect("got a null pointer")
}
#[allow(unsafe_code)]
unsafe fn AnyAttribute(&self, _: *mut JSContext) -> JSVal { NullValue() }
@ -164,8 +163,7 @@ impl TestBindingMethods for TestBinding {
#[allow(unsafe_code)]
unsafe fn ObjectAttribute(&self, cx: *mut JSContext) -> NonNull<JSObject> {
rooted!(in(cx) let obj = JS_NewPlainObject(cx));
assert!(!obj.is_null());
NonNull::new_unchecked(obj.get())
NonNull::new(obj.get()).expect("got a null pointer")
}
#[allow(unsafe_code)]
unsafe fn SetObjectAttribute(&self, _: *mut JSContext, _: *mut JSObject) {}

View file

@ -1123,8 +1123,8 @@ impl XMLHttpRequest {
unsafe fn arraybuffer_response(&self, cx: *mut JSContext) -> Option<NonNull<JSObject>> {
// Step 1
let created = self.response_arraybuffer.get();
if !created.is_null() {
return Some(NonNull::new_unchecked(created));
if let Some(nonnull) = NonNull::new(created) {
return Some(nonnull)
}
// Step 2