From 62b2a844fa8244f5c641b1a5bee1cdff9a5baed6 Mon Sep 17 00:00:00 2001 From: Lars Bergstrom Date: Tue, 4 Mar 2014 09:00:44 -0800 Subject: [PATCH 1/4] Rust upgrade --- Makefile.in | 2 +- layers.rs | 8 +- lib.rs | 20 +++-- platform/linux/surface.rs | 2 +- platform/macos/surface.rs | 4 +- rendergl.rs | 4 +- temp_rc.rs | 184 -------------------------------------- texturegl.rs | 8 ++ 8 files changed, 29 insertions(+), 203 deletions(-) delete mode 100644 temp_rc.rs diff --git a/Makefile.in b/Makefile.in index ad2f528..e411365 100644 --- a/Makefile.in +++ b/Makefile.in @@ -9,7 +9,7 @@ RUST_SRC=$(shell find $(VPATH)/. -type f -name '*.rs') all: librust-layers.dummy librust-layers.dummy: lib.rs $(RUST_SRC) - $(RUSTC) $(RUSTFLAGS) $< --lib --out-dir . + $(RUSTC) $(RUSTFLAGS) $< --crate-type=lib --out-dir . touch $@ rust-layers-test: lib.rs $(RUST_SRC) diff --git a/layers.rs b/layers.rs index 7746ff8..61f7538 100644 --- a/layers.rs +++ b/layers.rs @@ -13,8 +13,7 @@ use geom::matrix::{Matrix4, identity}; use geom::size::Size2D; use geom::rect::Rect; use std::cell::RefCell; -use std::ptr::to_unsafe_ptr; -use temp_rc::Rc; +use std::rc::Rc; pub enum Format { ARGB32Format, @@ -85,7 +84,7 @@ pub fn ContainerLayer() -> ContainerLayer { } struct ChildIterator { - priv current: Option, + current: Option, } impl Iterator for ChildIterator { @@ -187,7 +186,8 @@ impl ContainerLayer { assert!(child_common.parent.is_some()); match child_common.parent { Some(ContainerLayerKind(ref container)) => { - assert!(to_unsafe_ptr(container.borrow()) == to_unsafe_ptr(pseudo_self.borrow())); + assert!(container.borrow() as *ContainerLayer == + pseudo_self.borrow() as *ContainerLayer); }, _ => fail!(~"Invalid parent of child in layer tree"), } diff --git a/lib.rs b/lib.rs index 0c5b86d..f98cea5 100755 --- a/lib.rs +++ b/lib.rs @@ -11,25 +11,27 @@ #[feature(managed_boxes)]; -extern mod extra; -extern mod geom; -extern mod opengles; -extern mod std; +extern crate extra; +extern crate geom; +extern crate opengles; +extern crate std; +extern crate serialize; #[cfg(target_os="macos")] -extern mod core_foundation; +extern crate core_foundation; #[cfg(target_os="macos")] -extern mod io_surface; +extern crate io_surface; +#[cfg(target_os="macos")] +extern crate collections; #[cfg(target_os="linux")] -extern mod xlib; +extern crate xlib; #[cfg(target_os="android")] -extern mod egl; +extern crate egl; pub mod layers; pub mod color; -pub mod temp_rc; pub mod rendergl; pub mod scene; pub mod texturegl; diff --git a/platform/linux/surface.rs b/platform/linux/surface.rs index ce375f6..091aaf7 100644 --- a/platform/linux/surface.rs +++ b/platform/linux/surface.rs @@ -97,7 +97,7 @@ impl NativeCompositingGraphicsContext { fail!("Unable to locate a GLX FB configuration that supports RGBA."); } - let fbconfig = *ptr::offset(fbconfigs, 0); + let fbconfig = *fbconfigs.offset(0); let vi = glXGetVisualFromFBConfig(glx_display, fbconfig); (cast::transmute(vi), Some(fbconfig)) } diff --git a/platform/macos/surface.rs b/platform/macos/surface.rs index 671bf9f..6d1e67f 100644 --- a/platform/macos/surface.rs +++ b/platform/macos/surface.rs @@ -15,7 +15,7 @@ use core_foundation::boolean::CFBoolean; use core_foundation::dictionary::CFDictionary; use core_foundation::number::CFNumber; use core_foundation::string::CFString; -use extra::serialize::{Decoder, Encodable, Encoder}; +use serialize::{Decoder, Encodable, Encoder}; use geom::size::Size2D; use io_surface::{kIOSurfaceBytesPerElement, kIOSurfaceBytesPerRow, kIOSurfaceHeight}; use io_surface::{kIOSurfaceIsGlobal, kIOSurfaceWidth, IOSurface, IOSurfaceID}; @@ -24,7 +24,7 @@ use opengles::cgl::{CGLChoosePixelFormat, CGLDescribePixelFormat, CGLPixelFormat use opengles::cgl::{CGLPixelFormatObj, CORE_BOOLEAN_ATTRIBUTES, CORE_INTEGER_ATTRIBUTES}; use opengles::cgl::{kCGLNoError}; use opengles::gl2::GLint; -use std::hashmap::HashMap; +use collections::HashMap; use std::local_data; use std::ptr; diff --git a/rendergl.rs b/rendergl.rs index ea35983..5a03892 100755 --- a/rendergl.rs +++ b/rendergl.rs @@ -100,12 +100,12 @@ pub fn load_shader(source_string: &str, shader_type: GLenum) -> GLuint { compile_shader(shader_id); if get_error() != NO_ERROR { - println(format!("error: {:d}", get_error() as int)); + println!("error: {:d}", get_error() as int); fail!("failed to compile shader"); } if get_shader_iv(shader_id, COMPILE_STATUS) == (0 as GLint) { - println(format!("shader info log: {:s}", get_shader_info_log(shader_id))); + println!("shader info log: {:s}", get_shader_info_log(shader_id)); fail!("failed to compile shader"); } diff --git a/temp_rc.rs b/temp_rc.rs deleted file mode 100644 index 221ec95..0000000 --- a/temp_rc.rs +++ /dev/null @@ -1,184 +0,0 @@ -// Copyright 2013 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -/*! Task-local reference-counted boxes (`Rc` type) - -The `Rc` type provides shared ownership of an immutable value. Destruction is deterministic, and -will occur as soon as the last owner is gone. It is marked as non-sendable because it avoids the -overhead of atomic reference counting. - -The `downgrade` method can be used to create a non-owning `Weak` pointer to the box. A `Weak` -pointer can be upgraded to an `Rc` pointer, but will return `None` if the value has already been -freed. - -For example, a tree with parent pointers can be represented by putting the nodes behind `Strong` -pointers, and then storing the parent pointers as `Weak` pointers. - -*/ - -use std::cast::transmute; -use std::ops::Drop; -use std::cmp::{Eq, Ord}; -use std::clone::{Clone, DeepClone}; -use std::rt::global_heap::exchange_free; -use std::ptr::read_ptr; -use std::option::{Option, Some, None}; - -struct RcBox { - value: T, - strong: uint, - weak: uint -} - -/// Immutable reference counted pointer type -#[unsafe_no_drop_flag] -pub struct Rc { - priv ptr: *mut RcBox, -} - -impl Rc { - /// Construct a new reference-counted box - pub fn new(value: T) -> Rc { - unsafe { - Rc { - // there is an implicit weak pointer owned by all the - // strong pointers, which ensures that the weak - // destructor never frees the allocation while the - // strong destructor is running, even if the weak - // pointer is stored inside the strong one. - ptr: transmute(~RcBox { value: value, strong: 1, weak: 1 }), - } - } - } -} - -impl Rc { - /// Borrow the value contained in the reference-counted box - #[inline(always)] - pub fn borrow<'a>(&'a self) -> &'a T { - unsafe { &(*self.ptr).value } - } - - /// Downgrade the reference-counted pointer to a weak reference - pub fn downgrade(&self) -> Weak { - unsafe { - (*self.ptr).weak += 1; - Weak { ptr: self.ptr } - } - } -} - -#[unsafe_destructor] -impl Drop for Rc { - fn drop(&mut self) { - unsafe { - if self.ptr != 0 as *mut RcBox { - (*self.ptr).strong -= 1; - if (*self.ptr).strong == 0 { - read_ptr(self.borrow()); // destroy the contained object - - // remove the implicit "strong weak" pointer now - // that we've destroyed the contents. - (*self.ptr).weak -= 1; - - if (*self.ptr).weak == 0 { - exchange_free(self.ptr as *i8) - } - } - } - } - } -} - -impl Clone for Rc { - #[inline] - fn clone(&self) -> Rc { - unsafe { - (*self.ptr).strong += 1; - Rc { ptr: self.ptr } - } - } -} - -impl DeepClone for Rc { - #[inline] - fn deep_clone(&self) -> Rc { - Rc::new(self.borrow().deep_clone()) - } -} - -impl Eq for Rc { - #[inline(always)] - fn eq(&self, other: &Rc) -> bool { *self.borrow() == *other.borrow() } - - #[inline(always)] - fn ne(&self, other: &Rc) -> bool { *self.borrow() != *other.borrow() } -} - -impl Ord for Rc { - #[inline(always)] - fn lt(&self, other: &Rc) -> bool { *self.borrow() < *other.borrow() } - - #[inline(always)] - fn le(&self, other: &Rc) -> bool { *self.borrow() <= *other.borrow() } - - #[inline(always)] - fn gt(&self, other: &Rc) -> bool { *self.borrow() > *other.borrow() } - - #[inline(always)] - fn ge(&self, other: &Rc) -> bool { *self.borrow() >= *other.borrow() } -} - -/// Weak reference to a reference-counted box -#[unsafe_no_drop_flag] -pub struct Weak { - priv ptr: *mut RcBox, -} - -impl Weak { - /// Upgrade a weak reference to a strong reference - pub fn upgrade(&self) -> Option> { - unsafe { - if (*self.ptr).strong == 0 { - None - } else { - (*self.ptr).strong += 1; - Some(Rc { ptr: self.ptr }) - } - } - } -} - -#[unsafe_destructor] -impl Drop for Weak { - fn drop(&mut self) { - unsafe { - if self.ptr != 0 as *mut RcBox { - (*self.ptr).weak -= 1; - // the weak count starts at 1, and will only go to - // zero if all the strong pointers have disappeared. - if (*self.ptr).weak == 0 { - exchange_free(self.ptr as *i8) - } - } - } - } -} - -impl Clone for Weak { - #[inline] - fn clone(&self) -> Weak { - unsafe { - (*self.ptr).weak += 1; - Weak { ptr: self.ptr } - } - } -} - diff --git a/texturegl.rs b/texturegl.rs index 8326711..b016574 100644 --- a/texturegl.rs +++ b/texturegl.rs @@ -64,6 +64,14 @@ impl Drop for Texture { } } +// This Trait is implemented because it is required +// for Zero, but we should never call it on textures. +impl Add for Texture { + fn add(&self, _: &Texture) -> Texture { + fail!("Textures cannot be added."); + } +} + impl Zero for Texture { fn zero() -> Texture { Texture { From b7079223ba0ac4f231a92bf1522976ef2624388d Mon Sep 17 00:00:00 2001 From: Lars Bergstrom Date: Thu, 13 Mar 2014 19:30:23 -0500 Subject: [PATCH 2/4] Fix double-borrow failure --- layers.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/layers.rs b/layers.rs index 61f7538..aed98ea 100644 --- a/layers.rs +++ b/layers.rs @@ -139,9 +139,9 @@ impl ContainerLayer { pseudo_self.borrow().first_child.set(Some(new_child.clone())); - match pseudo_self.borrow().last_child.borrow().get() { - &None => pseudo_self.borrow().last_child.set(Some(new_child.clone())), - &Some(_) => {} + let should_set = pseudo_self.borrow().last_child.borrow().get().is_none(); + if should_set { + pseudo_self.borrow().last_child.set(Some(new_child.clone())); } }); } From f80f9569c928f219a115931cf90a9fdcd953e0d0 Mon Sep 17 00:00:00 2001 From: Josh Matthews Date: Fri, 14 Mar 2014 15:10:52 -0400 Subject: [PATCH 3/4] Warning police. --- platform/linux/surface.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platform/linux/surface.rs b/platform/linux/surface.rs index 091aaf7..722192d 100644 --- a/platform/linux/surface.rs +++ b/platform/linux/surface.rs @@ -93,7 +93,7 @@ impl NativeCompositingGraphicsContext { let mut configs = 0; let fbconfigs = glXChooseFBConfig(glx_display, screen, &fbconfig_attributes[0], &mut configs); - if (configs == 0) { + if configs == 0 { fail!("Unable to locate a GLX FB configuration that supports RGBA."); } From c29c94e8e14a87e3184d0273e025308d5d0a316e Mon Sep 17 00:00:00 2001 From: Lars Bergstrom Date: Sun, 16 Mar 2014 18:01:21 -0500 Subject: [PATCH 4/4] Rust upgrade for android --- platform/android/surface.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/platform/android/surface.rs b/platform/android/surface.rs index 17c3d39..72e240a 100644 --- a/platform/android/surface.rs +++ b/platform/android/surface.rs @@ -17,8 +17,8 @@ use opengles::gl2::{egl_image_target_texture2d_oes, TEXTURE_2D, glTexImage2D, BG use egl::egl::EGLDisplay; use egl::eglext::{EGLImageKHR, DestroyImageKHR}; use std::cast; +use std::mem; use std::ptr; -use std::util; use std::vec; use std::libc::c_void; @@ -140,7 +140,7 @@ impl NativeSurfaceMethods for NativeSurface { None => {}, Some(image_khr) => { DestroyImageKHR(graphics_context.display, image_khr); - util::replace(&mut self.image, None); + mem::replace(&mut self.image, None); } } self.mark_wont_leak()