diff --git a/components/util/dlist.rs b/components/util/dlist.rs index 14a1b5d4cdf..420a77a2698 100644 --- a/components/util/dlist.rs +++ b/components/util/dlist.rs @@ -66,3 +66,29 @@ pub fn split(list: &mut DList) -> DList { } } +/// Appends the items in the other list to this one, leaving the other list empty. +#[inline] +pub fn append_from(this: &mut DList, other: &mut DList) { + unsafe { + let this = mem::transmute::<&mut DList,&mut RawDList>(this); + let other = mem::transmute::<&mut DList,&mut RawDList>(other); + if this.length == 0 { + this.head = mem::replace(&mut other.head, None); + this.tail = mem::replace(&mut other.tail, ptr::null_mut()); + this.length = mem::replace(&mut other.length, 0); + return + } + + (*this.tail).next = match mem::replace(&mut other.head, None) { + None => return, + Some(mut head) => { + head.prev = this.tail; + Some(head) + } + }; + this.tail = mem::replace(&mut other.tail, ptr::null_mut()); + this.length += other.length; + other.length = 0; + } +} +