<commit_before>// Copyright 2013 The GLFW-RS Developers. For a full listing of the authors,
// refer to the AUTHORS file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! This example shows how managed objects can be accessed from callbacks

extern mod glfw;

use std::local_data;

static tls_key: local_data::Key<@mut TitleUpdater> = &local_data::Key;

struct TitleUpdater {
    window: @mut glfw::Window,
}

impl TitleUpdater {
    pub fn update(&self, title: &str) {
        self.window.set_title(title);
    }

    /* TLS management. */
    pub fn set(tu: @mut TitleUpdater) {
        local_data::set(tls_key, tu);
    }

    pub fn get() -> @mut TitleUpdater {
        do local_data::get(tls_key) |opt| {
            match opt {
                Some(x) => *x,
                None => fail!("Invalid TitleUpdater"),
            }
        }
    }
}

#[start]
fn start(argc: int, argv: **u8, crate_map: *u8) -> int {
    std::rt::start_on_main_thread(argc, argv, crate_map, main)
}

fn main() {
    do glfw::set_error_callback |_, msg| {
        printfln!("GLFW Error: %s", msg);
    }

    do glfw::start {
        let window = @mut glfw::Window::create(300, 300, "Move cursor in window", glfw::Windowed).unwrap();
        let title_updater = @mut TitleUpdater { window: window };
        TitleUpdater::set(title_updater); // Store in TLS.

        // Title updater must be in TLS and cannot be captured in the callback.
        do window.set_cursor_pos_callback |_, x, y| {
            TitleUpdater::get().update(fmt!("(%f %f)", x, y));
        }

        do window.set_key_callback |win, key, _, action, _mods| {
            if action == glfw::PRESS && key == glfw::KEY_ESCAPE {
                win.set_should_close(true);
            }
        }
        window.make_context_current();

        while !window.should_close() {
            glfw::poll_events();
        }
    }
}

<commit_msg>Modify managed-callbacks example to make the use case more obvious<commit_after>// Copyright 2013 The GLFW-RS Developers. For a full listing of the authors,
// refer to the AUTHORS file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! This example shows how managed objects can be accessed from callbacks

extern mod glfw;

use std::local_data;

static tls_key: local_data::Key<@mut State> = &local_data::Key;

struct State {
    priv pos: (float, float),
}

impl State {
    pub fn init(x: float, y: float) {
        local_data::set(tls_key, @mut State { pos: (x, y) });
    }

    pub fn update(x: float, y: float) {
        do local_data::get(tls_key) |opt| {
            opt.expect("Task-local state not initialized.").pos = (x, y);
        }
    }

    pub fn get_pos() -> (float, float) {
        do local_data::get(tls_key) |opt| {
            opt.expect("Task-local state not initialized.").pos
        }
    }
}

#[start]
fn start(argc: int, argv: **u8, crate_map: *u8) -> int {
    std::rt::start_on_main_thread(argc, argv, crate_map, main)
}

fn main() {
    do glfw::set_error_callback |_, msg| {
        printfln!("GLFW Error: %s", msg);
    }

    do glfw::start {
        let window = glfw::Window::create(300, 300, "Move cursor in window", glfw::Windowed).unwrap();
        State::init(0f, 0f);

        do window.set_cursor_pos_callback |_, x, y| {
            State::update(x, y);
        }

        do window.set_key_callback |win, key, _, action, _mods| {
            if action == glfw::PRESS && key == glfw::KEY_ESCAPE {
                win.set_should_close(true);
            }
        }
        window.make_context_current();

        while !window.should_close() {
            glfw::poll_events();
            window.set_title(format!("{:?}", State::get_pos()));
        }
    }
}


{'fraction_non_alphanumeric': 0.1255698300870286, 'fraction_numerical': 0.007045171985080812, 'mean_word_length': 2.60762331838565, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2123995', 'n_tokens_mistral': 1501, 'n_tokens_neox': 1477, 'n_words': 482}